Wrathi
02-19-2007, 10:22 AM
Hi all,
I have a working knowledge of MySQL currently, but I decided to try expand it a bit on my latest project, and I've hit a bit of a wall.
I have a table named `items`. It has the following fields:
`item_id`
`item_name`
`item_cost`
`item_quantity`
`item_desc`
`item_cat`
The main things to see here is that: item_quantity is the Quantity in stock. item_cat is the Category of the item.
What I'm trying to do is run a SELECT to find the total quantity of items in each category, then have the query give me the top 5 stocked categories so I can run them through a loop with mysql_fetch_array(). The query I'm using is:
SELECT `item_cat`,sum(`item_quantity`) AS `total_quantity` FROM `items` GROUP BY `item_cat` ORDER BY `total_quantity` DESC LIMIT 0,5
However, mysql_error() returns "Invalid use of group function".
I've looked up some tutorials on GROUP and sum, but neither show how they should be used together.
What am I doing wrong? :(
Thanks for reading,
Wrathi
I have a working knowledge of MySQL currently, but I decided to try expand it a bit on my latest project, and I've hit a bit of a wall.
I have a table named `items`. It has the following fields:
`item_id`
`item_name`
`item_cost`
`item_quantity`
`item_desc`
`item_cat`
The main things to see here is that: item_quantity is the Quantity in stock. item_cat is the Category of the item.
What I'm trying to do is run a SELECT to find the total quantity of items in each category, then have the query give me the top 5 stocked categories so I can run them through a loop with mysql_fetch_array(). The query I'm using is:
SELECT `item_cat`,sum(`item_quantity`) AS `total_quantity` FROM `items` GROUP BY `item_cat` ORDER BY `total_quantity` DESC LIMIT 0,5
However, mysql_error() returns "Invalid use of group function".
I've looked up some tutorials on GROUP and sum, but neither show how they should be used together.
What am I doing wrong? :(
Thanks for reading,
Wrathi