我有一个客户详细信息表,由ID标识,如下所示:
ID (the clientcode), Name, Details
我想从另一个包含销售信息的表中引用ID,并在查询中选择客户端的名称。
我只获取ID(客户端代码)的原始查询字符串是:
SELECT clientcode, SUM(sales) FROM inventory WHERE manufacturer='1'
GROUP BY client code ORDER BY SUM(sales) DESC
我还想选择客户端代码引用的客户端的名称。
我尝试了一些LEFT JOIN但无法使查询正常工作。
答案 0 :(得分:2)
SELECT i.clientcode
, c.name
, SUM(i.sales)
FROM inventory i
LEFT
JOIN clientdetails c
ON c.id = i.clientcode
WHERE i.manufacturer='1'
GROUP BY i.clientcode, c.name
ORDER BY SUM(i.sales) DESC
答案 1 :(得分:1)
这应该这样做
SELECT c.ID, c.name, SUM(i.sales)
FROM inventory i
JOIN clients c ON c.ID = i.clientcode
WHERE i.manufacturer='1'
GROUP BY c.ID, ORDER BY SUM(i.sales) DESC
答案 2 :(得分:1)
实现这一目标的简单查询是:
SELECT i.clientcode, d.name, SUM(i.sales) FROM inventory i, details d
WHERE i.manufacturer='1'
AND d.clientcode = i.clientcode
GROUP BY i.clientcode ORDER BY SUM(i.sales) DESC
答案 3 :(得分:1)
在这里,试试这个。
SELECT inventory.clientcode,
SUM(inventory.sales),
clientdetails.Name
FROM inventory
INNER JOIN clientdetails
ON inventory.clientcode = clientdetails.clientcode
WHERE inventory.manufacturer='1'
GROUP BY inventory.clientcode, clientdetails.Name
ORDER BY SUM(inventory.sales) DESC
答案 4 :(得分:0)
SELECT a.clientcode,
SUM(a.sales),
b.Name
FROM inventory a
INNER JOIN clientdetails b
ON a.clientcode = b.clientcode
WHERE a.manufacturer='1'
GROUP BY a.clientcode, b.Name
ORDER BY SUM(a.sales) DESC