tags:

views:

389

answers:

2

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.

+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
Yup, MySQL's fine w/this.
Alex Martelli
+3  A: 

Try this.

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