使用来自不同表的其他字段返回前10个MYSQL查询结果

时间:2016-08-26 08:44:35

标签: mysql

我需要根据收入增长返回前10名客户,同时还要显示订单数量和订单总重量。

首先,我们有客户表,用于存储客户帐户。 其次,我们有发票表,它存储每个订单的收入。这将用于获得前十名客户,因为他们按收入排名。 最后,我有订单表,它存储有关订单的其他详细信息,例如重量。

我试过了:

SELECT (c.account_number as Account, c.customer_name as Name, SUM(i.total_charge) as Revenue, COUNT(s.order) as NumberOfOrder, SUM(s.tot_weight) as Weight)
FROM t_customer as c
JOIN t_invoice as i ON c.account_number = i.account_number
JOIN t_shipment as s ON c.account_number = s.shipper_account 
WHERE s.ship_date BETWEEN '2016-08-01' AND '2016-08-31'
GROUP BY c.account_number
ORDER BY SUM(i.total_charge) DESC LIMIT 10

我认为我有正确但它只返回SQL错误(1241):操作数应该是1列。谢谢你的帮助。

1 个答案:

答案 0 :(得分:-1)

不要在您的选择中添加括号。

SELECT c.account_number as Account, c.customer_name as Name, SUM(i.total_charge) as Revenue, COUNT(s.order) as NumberOfOrder, SUM(s.tot_weight) as Weight
FROM t_customer as c
JOIN t_invoice as i ON c.account_number = i.account_number
JOIN t_shipment as s ON c.account_number = s.shipper_account 
WHERE s.ship_date BETWEEN '2016-08-01' AND '2016-08-31'
GROUP BY c.account_number
ORDER BY SUM(i.total_charge) DESC LIMIT 10

希望它会有所帮助。