我有 2 张桌子:
1.项目
+-------+---------+
|itemID | itemName|
+-----------------+
|1 | Item1 |
|2 | Item2 |
+-------+---------+
2.采购
+-----------+--------+--------+-----------+
|purchaseID | userID | itemID | itemAmount|
+-----------+--------+--------+-----------+
|1 | 1 | 1 | 3 |
|2 | 2 | 1 | 4 |
|3 | 1 | 2 | 5 |
+-----------+--------+--------+-----------+
我的 MySQL 代码的简化版本是这样的:
SELECT
items.itemID,
SUM(purchases.itemAmount)
FROM items
LEFT OUTER JOIN purchases ON items.itemID = purchases.itemID
我希望结果是这样的:
+-------+-----------+
|itemID | itemAmount|
+-------------------+
|1 | 7 |
|2 | 5 |
+-------+-----------+
但是,结果如下:
+-------+-----------+
|itemID | itemAmount|
+-------------------+
|1 | 12 |
+-------+-----------+
为什么会这样,我该如何解决?谢谢 :)