我有一个mysql表:
line_item_id order_id product_id qty
1 1 144 4
2 1 577 1
3 1 474 1
4 2 909 10
5 2 474 1
如何对此表进行排序,使其按照销售总数(例如909,144,474,577)的降序列表返回product_id?
答案 0 :(得分:3)
select product_id, sum(qty) as qty_sum
from table
group by product_id
order by qty_sum desc