表例如:取得跨多個表的總和
branch
----------------------------------
branchID | branchName | brMgrName|
----------------------------------
| 1 | a |Tom |
| 2 | b |Jeff |
| 3 | c |Lin |
----------------------------------
order
----------------------
orderID | branchID |
----------------------
| 1 | 2 |
| 2 | 2 |
| 3 | 3 |
----------------------
payment
--------------------------------
paymentID | orderID | p-amount |
--------------------------------
| 1 | 1 | 50.00 |
| 2 | 2 | 126.00 |
| 3 | 3 | 50.00 |
--------------------------------
,我想輸出是
-------------------------------------------
| branchName | brMgrName | Total Amount |
-------------------------------------------
| a | Tom | 0.00 |
| b | Jeff | 252.00 |
| c | Lin | 50.00 |
-------------------------------------------
我試圖確定如何從每個分支在3個不同的總結量表。
在輸出:
- BRANCHNAME '一' 沒有從順序表的任何值,
- BRANCHNAME 'B' 是由paymentID 1 & 2歸納起來,並且在同一用
- BRANCHNAME'
SELECT `order`.branchID, sum(`p-amount`)
FROM `order`
JOIN payment
ON `order`.orderID = payment.orderID
GROUP BY branchID
但是,當我嘗試下面的代碼,它重:C」
我已經成功地通過加入分支和支付表返回兩個分支名稱和總金額打開錯誤的值,只有1行被選中。
SELECT branchName, brMgrName, sum(`p-amount`)
FROM branch, payment
JOIN (SELECT `order`.branchID, sum(`p-amount`)
FROM `order`
JOIN payment
ON `order`.orderID = payment.orderID
GROUP BY branchID) b
ON b.branchID = branchID;
任何幫助,非常感謝。
「 - 」一表/列標識找麻煩 – Strawberry
@Strawberry謝謝指點出來。 –