This is a problem I run into often with database relations with MySQL. GROUP BY allows you to add up, average or pick the highest/lowest from all the values of a distinct field among otherwise duplicate rows. Great for doing arithmetic, but no use at all when what you want is a set of the values instead: such as a concatenated string.
Here's one specific (slightly abstracted) example:
I have a table, players, which stores the details of players attending a LAN party. It has the fields id and name.
players contains the following data:
1 John Doe
2 Jane Doe
I have another table, computers, which stores details of the computers each player has. It has the fields player_id (relates to the id field in players) and ip_address. One player may have an arbitrary number of computers.
computers contains the following data:
1 192.168.0.1
1 192.168.0.2
2 192.168.0.3
In other words, the player John Doe has brought two computers: one has the IP 192.168.0.1, and the other has the IP 192.168.0.2. Jane Doe has brought only one computer, which has the IP 192.168.0.3.
What I want to do is display a list of all the players, along with the ips of all the computers they have grouped under that player. And I want to do this in the SQL query itself with a GROUP BY.
There are GROUP BY functions that will give me the number of IP addresses John has; do arithmetic on John's IP addresses (which won't work, because it's a string); or give me the highest IP address, or the lowest. But there doesn't seem to be a function that will give all the IP addresses he has concatenated in a nice tidy string.
The workaround seems to be buffering the results and doing the grouping on the client-side (clumsy), or doing a sub-query for each player result to grab the set of computer IPs associated with them (slow for lots of results). Is there any way to do get sets of field values in a single query for an arbitrary number of relations?