GROUP_CONCAT()

Overview

Returns a concatenated string formed from the arguments.

USAGE

GROUP_CONCAT([DISTINCT] value[, value] ...
          [ORDER BY {unsigned_int | col | expr}
              [ASC | DESC][, col] ...]
          [SEPARATOR str_sep])

Argument Name

Description

DISTINCT

If the parameters start with DISTINCT clause, it specifies that duplicate values are first removed from the list of values before concatenating values. The default behavior is to concatenate all values when this parameter is not specified.

value

The values to be concatenated

ORDER BY

Sorts values in the result. The parameter to the ORDER BY clause may be an unsigned integer, a column name, or an expression.

ASC | DESC

Specifies whether sorting is to be performed in ascending (ASC clause) or descending (DESC clause) order. Default is ASC.

SEPARATOR

The separator to use between concatenated values. Default: ",".

DETAILS

GROUP_CONCAT() is an aggregate function that returns the concatenation of a set of values.

Any NULL values are ignored when joining the values.

If no value qualifies to be concatenated, which includes NULL values, the return value is NULL.

SYNONYMS

SCHEMA

PARAMETERS

SKYSQL

PRIVILEGES

EXAMPLES

Example Schema and Data

All of the examples are based on the inventory table:

CREATE TABLE inventory (
  item VARCHAR(20),
  category VARCHAR(20),
  count INT
);
INSERT INTO inventory VALUES
  ('Foo','in stock', 42), ('Foo','back order', 50),
  ('Bar','in stock', 10), ('Bar','back order', 60),
  ('Baz','in stock', 99), ('Baz','back order', 10),
  ('Foobie','in stock', 10), ('Barby','back order', NULL),
  ('BarBaz','in stock', NULL), ('BarBaz','back order', NULL);

Default Behavior

The default behavior for GROUP_CONCAT() is applied when DISTINCT clause is not specified. In this example, all values are used:

SELECT GROUP_CONCAT(item ORDER BY item ASC SEPARATOR ' ') AS GROUP_CONCAT_item
  FROM inventory;
+----------------------------------------------------+
| GROUP_CONCAT_item                                  |
+----------------------------------------------------+
| Bar Bar BarBaz BarBaz Barby Baz Baz Foo Foo Foobie |
+----------------------------------------------------+

DISTINCT Clause

Using GROUP_CONCAT() with the DISTINCT clause ignores duplicate values, so each value is added only once when concatenating. In this example, all distinct item count values are used:

SELECT GROUP_CONCAT(DISTINCT item ORDER BY item SEPARATOR ' ') AS GROUP_CONCAT_item
 FROM inventory;
+---------------------------------+
| GROUP_CONCAT_item               |
+---------------------------------+
| Bar BarBaz Barby Baz Foo Foobie |
+---------------------------------+

With GROUP BY

Using GROUP_CONCAT() with GROUP BY creates a separate concatenation for each group. In this example, GROUP BY category creates a separate concatenation for each unique category:

SELECT category, GROUP_CONCAT(item)
  FROM inventory
  GROUP BY category
  ORDER BY category;
+------------+---------------------------+
| category   | GROUP_CONCAT(item)        |
+------------+---------------------------+
| back order | Foo,Bar,Baz,Barby,BarBaz  |
| in stock   | Foo,Bar,Baz,Foobie,BarBaz |
+------------+---------------------------+

With NULL Values

When GROUP_CONCAT() acts on NULL values, those values are not included. In this example, some rows in the data set contain NULL values in the count column:

SELECT *
 FROM inventory
 WHERE item IN ('Foobie', 'Barby')
 ORDER BY count;
+--------+------------+-------+
| item   | category   | count |
+--------+------------+-------+
| Barby  | back order |  NULL |
| Foobie | in stock   |    10 |
+--------+------------+-------+

The following example computes a concatenation of the count column for the Foobie and Barby items:

SELECT GROUP_CONCAT(count)
 FROM inventory
 WHERE item IN ('Foobie', 'Barby');
+---------------------+
| GROUP_CONCAT(count) |
+---------------------+
| 10                  |
+---------------------+

With Only NULL Values

When GROUP_CONCAT() is acting only on NULL values, GROUP_CONCAT() returns NULL. In this example, all rows in the data set contain NULL values in the count column:

SELECT *
 FROM inventory
 WHERE item = 'BarBaz'
 ORDER BY count;
+--------+------------+-------+
| item   | category   | count |
+--------+------------+-------+
| BarBaz | in stock   |  NULL |
| BarBaz | back order |  NULL |
+--------+------------+-------+

The following example performs a concatenation of the count column for the BarBaz item:

SELECT GROUP_CONCAT(count)
 FROM inventory
 WHERE item = 'BarBaz';
+---------------------+
| GROUP_CONCAT(count) |
+---------------------+
| NULL                |
+---------------------+

ERROR HANDLING

FEATURE INTERACTION

RESPONSES

DIAGNOSIS

ISO 9075:2016

CHANGE HISTORY

Release Series

History

23.09

  • Present starting in MariaDB Xpand 23.09.1.

6.1

  • Present starting in MariaDB Xpand 6.1.0.

6.0

  • Present starting in MariaDB Xpand 6.0.3.

5.3

  • Present starting in MariaDB Xpand 5.3.13.

Release Series

History

6.0

  • Present starting in MariaDB Xpand 6.0.3.

5.3

  • Present starting in MariaDB Xpand 5.3.13.

Release Series

History

6.1

  • Present starting in MariaDB Xpand 6.1.0.

EXTERNAL REFERENCES