MySQL添加具有特定id的列

时间:2016-07-10 14:21:54

标签: mysql sql

我有三张桌子:

  • 一种是存储id和名称的某种客户表。
  • 第二个是项目表,其中存储了ID,名称和价格。
  • 第三个将两者的ID与数量相结合。

我的问题是我如何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

1 个答案:

答案 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;
相关问题