GROUP_CONCAT
Syntax
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 BLOB or TEXT. The choice between binary or non-binary types depends from the input.
The full syntax is as follows:
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
. This was not possible prior to MariaDB 10.3.3.
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 () 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).
From MariaDB 10.3.3, 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?