Category

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

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <em> <strong> <cite> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.
© 2008-2009. Konstantin Artemov