Web Hosting Talk







View Full Version : SQL question: Select COUNT(*) and then order by COUNT


riverpast
03-14-2007, 02:34 PM
This works:
select product_id, count(*) from order_line group by product_id

It shows the product and how many sales each product has.

However, I want to sort by the product sales, so I am trying:
select product_id, count(*) from order_line group by product_id order by count(*)

It doesn't like "order by count(*)". I tried many variations and just couldn't figure out how to make it sort by the count

Of course, "orber by product_id" would work.

How do I make it sort by the COUNT()?

By the way, I am using MySQL

foobic
03-14-2007, 03:49 PM
SELECT product_id, count(*) AS total
FROM order_line
GROUP BY product_id
ORDER BY total

riverpast
03-14-2007, 03:56 PM
Thanks. Exactly what I needed.