我有order_idproduct_id和数量字段的OrdersProducts表。我想知道最流行的产品,所以我怎么能总结的数量字段每个PRODUCT_ID分开,然后订购的款项?任何指针将不胜感激。我使用MySQL 5.3,谢谢。

有帮助吗?

解决方案

尝试。

select product_id, sum(quantity) As ProductQtySum
from OrdersProducts 
group by product_id 
order by ProductQtySum Desc

其他提示

不是100%肯定MySQL会喜欢这样 - 但它相当标准的SQL

SELECT product_id, sum(quantity)
FROM OrdersProducts 
GROUP BY product_id
ORDER BY sum(quantity) DESC
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top