Update a mysql table column with sum of joined table columns value? -


आदेश तालिका

  आईडी ऑर्डर डेट कुल   

आइटम तालिका

  आईडी मूल्य मात्रा आदेशआईडी (एफकी -> ऑर्डर। आईडी)   

ऑर्डर के कई आइटम हैं।

mysql में एसक्यूएल, order.total को अपडेट करने का एक तरीका है, ताकि यह उत्पाद की कुल राशि item.price और item.quantity के लिए हो प्रत्येक आइटम?

वह है -

  आदेश। Each: order.items.each: sum + = item.price * item.quantity; Order.total = sum;   

मैं इसके लिए एक सरल लिपि लिख सकता हूं, लेकिन मैं यह जानना चाहता हूं कि यह कैसे एसक्यूएल में करना है।

हाँ आप यह एक update / शामिल के साथ कर सकते हैं। आपको कुल प्राप्त करने के लिए आइटम को एकत्रित करने की आवश्यकता है और फिर परिणाम को वापस करने के लिए आदेश में अपडेट करें:

  ऑर्डर ऑर्डर करें जुड़ें (i.orderid, sum (i.price * i.quantity) के रूप में मैं i.orderid द्वारा आइटम्स के कुल में) o.orderid = oi.orderid सेट o.total = oi.total;    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -