GROUP_CONCAT
Complete GROUP_CONCAT reference for MariaDB. Complete function guide with syntax, parameters, return values, and usage examples for production use.
Syntax
GROUP_CONCAT(expr)Description
This function returns a string result with the concatenated non-NULL values from a group. If any expr in GROUP_CONCAT evaluates to NULL, that tuple is not present in the list returned by GROUP_CONCAT.
It returns NULL if all arguments are NULL, or there are no matching rows.
The maximum returned length in bytes is determined by the group_concat_max_len server system variable, which defaults to 1M.
If group_concat_max_len <= 512, the return type is VARBINARY or VARCHAR; otherwise, the return type is the smallest BLOB or TEXT type that can hold a result of that length. With the default group_concat_max_len, that is MEDIUMBLOB or MEDIUMTEXT. The choice between binary or non-binary types depends on the input.
If the result is longer than group_concat_max_len, it is truncated to that length, and a warning is issued:
Warning (Code 1260): Row 2 was cut by group_concat()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]
[LIMIT {[offset,] row_count | row_count OFFSET offset}])DISTINCT eliminates duplicate values from the output string.
ORDER BY determines the order of returned values.
SEPARATOR specifies a separator between the values. The default separator is a comma (,). It is possible to avoid using a separator by specifying an empty string.
LIMIT
The LIMIT clause can be used with GROUP_CONCAT.
Memory Usage with ORDER BY
With ORDER BY, the rows of each group are sorted in memory. The memory used for sorting is limited to the larger of these two values:
group_concat_max_len, or max_allowed_packet if that is smaller
The smaller of tmp_memory_table_size and max_heap_table_size
With the default settings, the limit is 16 MB. The limit is never set so low that the sort cannot hold a few rows. Values of BLOB and TEXT arguments are not counted toward it.
When the sort reaches two thirds of the limit, the server discards the rows that come after the end of the result in ORDER BY order and can therefore never be returned. If the rows that belong to the result do not fit into a third of the limit, some of them are left out of the result, and the same warning as for a truncated result is issued.
Before MariaDB 13.2, the rows were only discarded once the strings being sorted added up to about 1024 times group_concat_max_len, which is 1 GB with the default settings, so a large group could use a lot of memory.
JSON_ARRAYAGG() is implemented on top of GROUP_CONCAT(), so JSON_ARRAYAGG() with ORDER BY has the same memory limit and issues the same warning.
Examples
Get a readable list of MariaDB users from the mysql.user table:
In the former example, DISTINCT is used because the same user may occur more than once. The new line (\n) used as a SEPARATOR makes the results easier to read.
Get a readable list of hosts from which each user can connect:
The former example shows the difference between the GROUP_CONCAT's ORDER BY (which sorts the concatenated hosts), and the SELECT's ORDER BY (which sorts the rows).
LIMIT can be used with GROUP_CONCAT, so, for example, given the following table:
the following query:
can be more simply rewritten as:
NULLS:
See Also
This page is licensed: GPLv2, originally from fill_help_tables.sql
Last updated
Was this helpful?

