Question

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.

Was it helpful?

Solution

Try this.

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

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top