输入数据:
“卡”表:
cardID名称
1个
2个
3℃
“数量”表(每个卡可以有多个条目,具体取决于[当天]到达的数量):
cardID数量
1 1
1 7
3 2
3 5
3 1
要求:
SELECT Card.cardID, SUM(Quantity.Quantity)
FROM Card
LEFT JOIN Quantity ON Card.cardID = Quantity.cardID
GROUP BY Card.cardID WITH ROLLUP
返回:
cardID SUM(...
1 ...
我需要它返回:
cardID SUM(...
1 ...
1 ...
2 0
3 ...
3 ...
3 ...
我究竟做错了什么?
最佳答案
您需要同时按数量和cardID进行分组。
SELECT Card.cardID, SUM(Quantity.Quantity)
FROM Card, Quantity
WHERE Card.cardID = Quantity.cardID
GROUP BY Card.cardID, Quantity.Quantity WITH ROLLUP
关于mysql - MySQL:SELECT + SUM()仅返回一条记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35339026/