MySQL GROUP_CONCAT function

MySQL GROUP_CONCAT function

tudip-logo

Tudip

24 June 2016

Lets say there is a table named fictional_characters, that looks like:

id          id_novel               character_name
1          1100                        Harry
2          1100                        Ron
3          1100                        Hermione

4          1101                        Frodo
5          1101                        Sam

 
Time and again you want to display them like:
 
1100  Harry, Ron, Hermione
1101 Frodo, Sam

 

This can be handled directly in the DB by writing a query like:
 

SELECT  id_novel,
GROUP_CONCAT(character_name)
FROM  fictional_characters
GROUP BY id_novel;

Check the complete reference of GROUP_CONCAT at:
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

search
Blog Categories
Request a quote