/    Sign up×
Community /Pin to ProfileBookmark

Help with MySQL Grouping Query

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.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 13.2006 — [code=php]
$query = "SELECT COUNT(*) AS total_count, color FROM table
WHERE id = '".$_GET['id']."' GROUP BY color ORDER BY total_count DESC";
[/code]
Copy linkTweet thisAlerts:
@legendxauthorDec 13.2006 — much appreciated..

but it will still only display 7 results.
Copy linkTweet thisAlerts:
@NogDogDec 13.2006 — much appreciated..

but it will still only display 7 results.[/QUOTE]

Well, perhaps there are only 7 unique color values associated with the id value in your WHERE clause?
×

Success!

Help @legendx spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...