Click to See Complete Forum and Search --> : Help with MySQL Grouping Query


legendx
12-13-2006, 12:58 AM
I have a table that contains a string in a row. What I want is to have the query return the number of rows that contain the same string in descending order. So like if I had a table like:

----------
|1 | Blue |
|2 | Red |
|3 | Red |
|4 | Blue |
|5 | Blue |
|6 | Gold |
|7 | Blue |
|8 | Gold |
|9 | Red |
----------
I want to echo:
There are 5 Blue
There are 3 Red
There are 2 Gold

Right now This is what my query looks like:
SELECT COUNT(id), color FROM table WHERE id = '".$_GET['id']."' GROUP BY color

But this almost works for me. It outputs something like
There are 3 Red
There are 5 Blue
There are 2 Gold

But it only returns a maximum of 7 rows and they are not in order by COUNT(id)
I've tried ORDER BY COUNT(id) DESC but mysql says invalid grouping method and also LIMIT 10 to try to coax more results from the query but to no avail.

NogDog
12-13-2006, 01:37 AM
$query = "SELECT COUNT(*) AS total_count, color FROM table
WHERE id = '".$_GET['id']."' GROUP BY color ORDER BY total_count DESC";

legendx
12-13-2006, 11:16 AM
much appreciated..

but it will still only display 7 results.

NogDog
12-13-2006, 12:16 PM
much appreciated..

but it will still only display 7 results.
Well, perhaps there are only 7 unique color values associated with the `id` value in your WHERE clause?