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 valuesKeywordOptional
- expr
- Expression whose values are joined
- ORDER BY sort_expression [ASC | DESC]
- Order of values in the string; without it the order is not guaranteedOptional
- sort_expression
- Column or expression to sort by
- ASC | DESC
- Direction, ASC by defaultKeywordOptionalASCDESC
- SEPARATOR str
- Separator, a comma without a space by defaultOptional
- str
- Separator string; '' for no separatorVARCHAR
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;Unique statuses separated by commas
MySQL 8.1
SELECT GROUP_CONCAT(
DISTINCT status
ORDER BY status SEPARATOR ', '
) AS statuses
FROM FamilyMembers;Names by status
MySQL 8.1
SELECT status,
GROUP_CONCAT(
member_name
ORDER BY member_name SEPARATOR ', '
) AS members
FROM FamilyMembers
GROUP BY status;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.