我有三张桌子:
我的问题是我如何SELECT
以他想要购买的所有商品的总价格为客户命名。
SELECT
first.name, third.quantity * second.price
FROM
first
LEFT JOIN
third ON third.fID = first.ID
LEFT JOIN
second ON second.ID = third.sID ;
这就是我所拥有的。它选择总价格的所有商品,但我需要为每个客户添加它们。
E.g。它返回:
customer1 5000
customer2 100
customer1 1000
但我想:
customer1 6000
customer2 100
答案 0 :(得分:2)
您似乎想要使用GROUP BY
:
SELECT first.name, SUM(third.quantity * second.price)
FROM first LEFT JOIN
third
ON third.fID = first.ID LEFT JOIN
second
ON second.ID = third.sID
GROUP BY first.name;