Skip to content

Instantly share code, notes, and snippets.

@BrechtBonte
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrechtBonte/5b1103dd28245462f09a to your computer and use it in GitHub Desktop.
Save BrechtBonte/5b1103dd28245462f09a to your computer and use it in GitHub Desktop.
example of MySQL GROUP_CONCAT

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment