I have an OrdersProducts tables with order_id
, product_id
, and quantity fields. I'd like to know the most popular products, so how can I sum the quantity field for each product_id separately and then order the sums? Any pointers would be appreciated. I'm using MySQL 5.3, thanks.
views:
389answers:
2
+3
A:
Not 100% sure MySQL will like this - but its pretty standard SQL
SELECT product_id, sum(quantity)
FROM OrdersProducts
GROUP BY product_id
ORDER BY sum(quantity) DESC
russau
2009-05-30 05:22:31
Yup, MySQL's fine w/this.
Alex Martelli
2009-05-30 05:55:55
+3
A:
Try this.
select product_id, sum(quantity) As ProductQtySum
from OrdersProducts
group by product_id
order by ProductQtySum Desc
James
2009-05-30 05:24:53