GROUP_CONCAT() Function
Group concat function is used to join the columns with delimiter. By default it is taking comma(,) delimiter.
Usage
-
Join columns data with delimiter
-
Order by with joining data
-
Distinct the joining data values.
|
User_info table has id and username columns.
id |
username |
1 |
john |
2 |
peter |
3 |
raja |
4 |
Peter |
|
How to use GROUP_CONCAT() function
-- ouput
-- john,peter,raja,Peter
select group_concat(first_name) from user_info;
-- output
-- john,peter,raja
select group_concat(DISTINCT username) from user_info;
-- output
-- john|peter|raja|Peter
select group_concat(username SEPARATOR "|") from user_info;
-- output
-- john|peter|Peter|raja
select group_concat(username ORDER BY username desc SEPARATOR "|") from user_info;
|