我的數據庫看起來如下MySQL的加入和多列在選擇
表名稱:訂單明細
id oid pid pquantity pprice
--------------------------------------
1 1 5 2 10
2 1 6 3 5
3 1 7 1 20
5 2 8 1 5
6 2 9 1 5
7 3 5 5 10
表名稱:訂單
id odiscount oshipping
----------------------------
1 5 5
2 0 5
3 0 5
我想發票金額每個訂單。 (pquantity*pprice)-odiscount+oshipping
。棘手的部分是,每個訂單可以有多個條目按順序細節。所以我無法弄清楚如何處理這個問題。最終結果應該是
oid total
1 55
2 15
3 55
我試過這個使用下面的SQL,但我無法弄清楚如何獲取多個行的細節到帳戶。
SELECT SUM((orderdetails.pprice*orderdetails.pquantity) - orders.odiscount + orders.oshipping) FROM orders LEFT JOIN orderdetails ON orderdetails.oid = orders.id GROUP BY orders.id
使用子查詢有什麼問題? – sagi
我同意這可能是要走的路,但我對評論的態度非常困惑。 – Strawberry
@Strawberry Sagi經常在我的答案上留下一大堆評論,創造出一無所有的談話,希望他的回答看起來比原來更加原始,因此比其他人更加正確。評論:這種策略似乎工作得很好(雖然我不使用它)。 –