PDA

View Full Version : understanding "GROUP BY"


Lionel
12-20-2005, 12:07 AM
I've got 32 world cup teams separated in 8 groups of 4

I am trying to display them by group

But the below query returns only the last group. When I remove the GROUP BY clause, they all display


$currentteams = $DB_site->query("
SELECT *
FROM " . TABLE_PREFIX . "cclpinternational_teams
WHERE disabled=1 AND name!='divadmin' AND division='$thedivision'
GROUP BY 'groupname'
ORDER BY name
");

What else one must know in order to get GROUP BY to display as needed?


I am really stucked. Can someone please give me a hint on how to proceed?
I managed the Group By clause to display how I wanted. However, while there is more than one element per group (in that case it should be four), it displays only one.


It consists of 8 groups with 4 teams each. I was hoping to be able to display all in same group together. I tried GROUP_CONCAT but that gives me an error.

Zubaz
12-21-2005, 11:46 PM
GROUP BY is kind of a misnomer. It'll group your results, but not in a fashion that is useful outside of finding sums or average. What you're looking for is a complex ORDER BY I think.

Try changing your order clause to
ORDER BY `groupname`,`name`
This will sort by groupname, then by name within that.

Maybe I didn't get what you're trying to do, but I tried to do a lot of stuff like this before and you really can't with a single group by query.

Lionel
12-21-2005, 11:50 PM
I got around it by displaying one group per page.

Zubaz
12-27-2005, 11:52 PM
Your code needs to detect when the group changes since you're doing a per team call. You could call each of your groups, then call each team associated with it, but that's more queries.