Page 1 of 1

MYSQL sorting problem

Posted: Sat May 27, 2006 11:26 am
by number
Hey,
I'm trying to sort a table( which contains order details eg productname/quantity/customers details) by the Sum of the quantities for each particular product.
Basically, I'm trying to output the three most popular products which have been ordered.

I'm trying to use:

Code: Select all

$query1 = "SELECT DISTINCT productName FROM shoeplanetorders ORDER BY SUM(quantity) DESC";
         $result1=mysql_query($query1);
which the MYSQL query browser says is "Invalid use of Group function".


I'm wondering if anyone can help me to return the most popular products, by the total sum of the quanties purchased? By leaving out the "SUM()" around the ORDER BY quantitity statement, it simply returns the products by the order of decreasing size of an individual order. Thanks in advance.

Posted: Sat May 27, 2006 11:49 am
by hawleyjr

Code: Select all

$query1 = "SELECT DISTINCT productName,SUM(quantity) as sumval FROM shoeplanetorders ORDER BY sumval DESC";