我正在尝试做最畅销的系统,但我在SQL语句上遇到了一些问题。这是我的SQL:
select sp.name, pr.productName, op.productID, SUM(op.orderQuantity) AS totalQty from sm_orderedproduct op
INNER JOIN sm_payment p ON op.orderID = p.orderID
INNER JOIN sm_product pr ON op.productID = pr.productID
INNER JOIN sm_sellerproduct sp ON sp.productID = pr.productID
WHERE MONTH(str_to_date( dateOfPurchase, '%d/%m/%Y' )) = 7
GROUP BY sp.name, pr.productName, op.productID
ORDER BY SUM(op.orderQuantity) DESC LIMIT 4;
使用此SQL语句,我得到重复的卖家名称。例如
卖方Dean售出97电视
卖家Rk售出20桌面
卖方迪恩再次卖掉了15个硬盘
卖家Gab卖了2张木桌
正如您所看到的,由于销售的产品不同,卖方Dean重复了两次。我想这样做:
卖方Dean销售了112种产品
卖方Rk售出20件产品
卖家Gab销售2件产品
卖家卖的是什么?产品
我相信我的group by子句出了点问题。提前谢谢。
答案 0 :(得分:3)
您需要通过以下方式从查询/组中删除与产品相关的信息:
select sp.name, sum(op.orderQuantity) AS totalQty
from sm_orderedproduct op
inner join sm_payment p ON op.orderID = p.orderID
inner join sm_product pr ON op.productID = pr.productID
inner join sm_sellerproduct sp ON sp.productID = pr.productID
where MONTH(str_to_date( dateOfPurchase, '%d/%m/%Y' )) = 7
group by sp.name
order by sum(op.orderQuantity) desc
limit 4;