MySQL: Group Concatenation

One of those small thing that are not essential but yet very helpful - GROUP_CONCAT which returns grupped rows as one string with selected separator (comma by default). EG:

SELECT GROUP_CONCAT(ID), type FROM `User` GROUP BY type;

This will return something like:
1,2,4,12 | admin
3,5,6,7,8,9,10,11 | user
SELECT GROUP_CONCAT(DISTINCT County ORDER BY Country SEPARATOR '; '), type FROM `User` GROUP BY type;

The result may be like this:
Ukraine; Russia; USA | admin
Russia; Canada; USA | user

Referenced from : http://www.soel.org.ua/programming/databases/mysql-group-concatenation

Comments

Popular posts from this blog

How to create unicode files using VBA?

JAVA: Set filename using setHeader()