Function Reference

GROUP_CONCAT

PostgreSQL equivalent:

Joins values from a group of rows into one string with a separator.

MySQL 8.1
GROUP_CONCAT([DISTINCT] expr [ORDER BY sort_expression [ASC | DESC]] [SEPARATOR str])
  • [ ]optional part
  • a | bone of the options
DISTINCT
Join only unique values
KeywordOptional
expr
Expression whose values are joined
ORDER BY sort_expression [ASC | DESC]
Order of values in the string; without it the order is not guaranteed
Optional
sort_expression
Column or expression to sort by
ASC | DESC
Direction, ASC by default
KeywordOptional
ASCDESC
SEPARATOR str
Separator, a comma without a space by default
Optional
str
Separator string; '' for no separator
VARCHAR

Return value

A string; NULLs are skipped, NULL if all values are NULL

Examples

All names in one string

MySQL 8.1
SELECT GROUP_CONCAT(member_name)
FROM FamilyMembers;
GROUP_CONCAT(member_name)
Headley Quincey,Flavia Quincey,Andie Quincey,Lela Quincey,Annie Quincey,Ernest Forrest,Constance Forrest,Wednesday Addams

Unique statuses separated by commas

MySQL 8.1
SELECT GROUP_CONCAT(
		DISTINCT status
		ORDER BY status SEPARATOR ', '
	) AS statuses
FROM FamilyMembers;
statuses
daughter, father, mother, son

Names by status

MySQL 8.1
SELECT status,
	GROUP_CONCAT(
		member_name
		ORDER BY member_name SEPARATOR ', '
	) AS members
FROM FamilyMembers
GROUP BY status;
statusmembers
daughterAnnie Quincey, Lela Quincey, Wednesday Addams
fatherErnest Forrest, Headley Quincey
motherConstance Forrest, Flavia Quincey
sonAndie Quincey

Details

Without ORDER BY, values come in the order the server read them, and it may change from run to run. By default, the result is truncated to 1024 bytes (the group_concat_max_len variable) with a warning, not an error.

See also