GROUP_CONCAT
GROUP_CONCAT(expr)
This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values.
Syntax:
GROUP_CONCAT([DISTINCT] expr [,expr …]
[ORDER BY {unsigned_integer | col_name | expr}
[ASC | DESC] [,col_name …]]
[SEPARATOR str_val])
Example:
SELECT student_name,GROUP_CONCAT(test_score)
FROM student
GROUP BY student_name;
SELECT student_name,
GROUP_CONCAT(DISTINCT test_score ORDER BY test_score DESC SEPARATOR ‘ ‘)
FROM student
GROUP BY student_name;
In MySQL, you can get the concatenated values of expression combinations. You can eliminate duplicate values by using DISTINCT. If you want to sort values in the result, you should use 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. The default is ascending order; this may be specified explicitly using the ASC keyword.
SEPARATOR is followed by the string value that should be inserted between values of result. The default is a comma (’,'). You can remove the separator altogether by specifying SEPARATOR ‘’.
You can set a maximum allowed length with the group_concat_max_len system variable.
If a maximum length has been set, the result is truncated to this maximum length.
