输入数据:
“卡”表:
cardID名称
1 A
2 B
3 C
“数量”表(每张卡可以有多个条目,具体取决于[当天]到达的数量):
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 ...
我做错了什么?
答案 0 :(得分:1)
您需要按数量和cardID分组。
SELECT Card.cardID, SUM(Quantity.Quantity)
FROM Card, Quantity
WHERE Card.cardID = Quantity.cardID
GROUP BY Card.cardID, Quantity.Quantity WITH ROLLUP