Using GROUP_CONCAT to simplify your life Print

  • 0

The GROUP_CONCAT function returns a comma separated list of all non-null values that were found in the query.  This is extremely useful when you want to pull a sub-set of related information during within a query, for example:

SELECT `user`.*
, (SELECT GROUP_CONCAT(`usergroup_id`) FROM `usergroup_xref` WHERE `usergroup_xref`.`user_id` = `user`.`id`) AS `usergroup_ids`
FROM `users` AS `user`

In the above example you could retrieve all of the users group ids along with the user row within a single query.

Was this answer helpful?

« Back