MYSQL sorting problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
number
Forum Newbie
Posts: 3
Joined: Thu Apr 27, 2006 11:44 am

MYSQL sorting problem

Post 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.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Code: Select all

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