Last week I stumbled over a really useful function in MySQL: group_concat allows you to concatenate the data of one column of multiple entries by grouping them by one field field. You can choose the separator to use for the concatenation. The full syntax is as follows:
GROUP_CONCAT([DISTINCT] expr [,expr ...] [ORDER BY {unsigned_integer | col_name | expr} [ASC | DESC] [,col_name ...]] [SEPARATOR str_val])
According to the MySQL documentation, the function returns a string result with the concatenated non-NULL
values from a group. It returns NULL
if there are no non-NULL
values. To eliminate duplicate values, use the DISTINCT
clause. To sort values in the result, use the ORDER BY
clause. To sort in reverse order, add the DESC
(descending) keyword to the name of the column you are sorting by in the ORDER BY
clause.
Continue reading ‘MySQL: group_concat allows you to easily concatenate the grouped values of a row’