题
我有order_id
,product_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
不隶属于 StackOverflow