A small example of the GROUP_CONCAT function in MySQL
Say we have 2 tables:
People:
id | name |
---|---|
1 | John |
2 | Tim |
and Hobbies:
id | person | name |
---|---|---|
1 | 1 | programming |
2 | 2 | going out |
3 | 2 | gaming |
If we want to list each person and his hobbies, we can do so with the following query:
SELECT people.name, GROUP_CONCAT(hobbies.name SEPARATOR ', ') as hobbies
FROM people INNER JOIN hobbies ON hobbies.person = people.id
GROUP BY people.id
This will give us the following result:
name | hobbies |
---|---|
John | programming |
Tim | going out, gaming |