©SQL :: Manager
HomeProgram InformationTechnical detailsFree downloadDonate for a cup of coffee
myway SQL Manager for MySQL and MariaDB, Download on Uptodown
SQL functionMySQLMariaDB

The GROUP_CONCAT SQL Function in MySQL and MariaDB - Concatenation of strings

GROUP_CONCATSyntax:GROUP_CONCAT([DISTINCT] Expression [, Expression ...] [ORDER BY ... [ASC | DESC]] [SEPARATOR ...] [LIMIT ...])
Return value:CHAR
Function type:Aggregate function
Function Description

The GROUP_CONCAT() function returns a string result containing the concatenated strings from a group.

Multiple expressions can be specified as arguments, separated by commas.

NULL values ​​are ignored. SET and ENUM columns are concatenated based on their string values.

Using a DISTINCT clause results in concatenation of only distinct strings.

By using the ORDER BY clause, the values ​​can be sorted before concatenation.

With the SEPARATOR option, a separator for concatenating the values ​​can be selected.

The LIMIT clause can be used to limit the result set.

Although the function is an aggregate function, it cannot be used in a GROUP BY clause.

GROUP_CONCAT() cannot be used as a window function.

The GROUP_CONCAT() function returns NULL if no result is found.


It should be noted that the length of the concatenated character strings is limited. The default limit in MySQL is 1024 and in MariaDB 1048576 characters, which can be changed using the group_concat_max_len system variable.

SQL Examples for the GROUP_CONCAT function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41


DROP TABLE IF EXISTS `test_table`;
    
CREATE TABLE `test_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `type` enum('First','Second','Third','Last'DEFAULT NULL,
  `set` set('First','Second','Third','Last'DEFAULT NULL,
  `num1` int(11) NOT NULL DEFAULT 0,
  `num2` int(11) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;

INSERT INTO `test_table` (`id`,`name`,`type`,`set`,`num1`,`num2`VALUES 
(1,'Art 1','First','Last',1,7),
(2,'Art 2','Second','First,Last',3,7),
(3,'Art 3','Second','Second,Last',5,7),
(4,'Art 4','Third','First,Second,Last',5,7),
(5,'Art 5','Last','First,Last',7,15),
(6,'Art 6','First','First,Last',7,15),
(7,'Art 7','Third','First,Third,Last',9,15),
(8,'Art 8','Last','Second,Third,Last',9,15);

select * from `test_table`;

select group_concat(name), group_concat(`type`), group_concat(distinct `type`)  from test_table;

select group_concat(name order by `type` asc separator ' - ')  from test_table;

select group_concat(name order by name desc separator ' - ')  from test_table;

select group_concat(name order by name desc separator ' - ' limit 3, 3)  from test_table;

select group_concat(name order by name desc separator ' - ' limit 3 offset 3)  from test_table;

select group_concat(name, '.'`type` order by `type` desc, name asc separator ' - ' limit 2, 6)  from test_table;

SELECT group_concat(null);

id
name
type
set
num1
num2
int(11)varchar(20) LATIN1enum LATIN1set LATIN1int(11)int(11)
1Art 1FirstLast17
2Art 2SecondFirst,Last37
3Art 3SecondSecond,Last57
4Art 4ThirdFirst,Second,Last57
5Art 5LastFirst,Last715
6Art 6FirstFirst,Last715
7Art 7ThirdFirst,Third,Last915
8Art 8LastSecond,Third,Last915
group_concat(name)
group_concat(`type`)
group_concat(distinct `type`)
text / blob(1048576) BINARYtext / blob(1048576) BINARYtext / blob(1048576) BINARY
Art 1,Art 2,Art 3,Art 4,Art 5,Art 6,Art 7,Art 8First,Second,Second,Third,Last,First,Third,LastFirst,Second,Third,Last
group_concat(name order by `type` asc separator ' - ')
text / blob(1048576) BINARY
Art 6 - Art 1 - Art 3 - Art 2 - Art 7 - Art 4 -...
group_concat(name order by name desc separator ' - ')
text / blob(1048576) BINARY
Art 8 - Art 7 - Art 6 - Art 5 - Art 4 - Art 3 -...
group_concat(name order by name desc separator ' - ' limit 3, 3)
text / blob(1048576) BINARY
Art 5 - Art 4 - Art 3
group_concat(name order by name desc separator ' - ' limit 3 offset 3)
text / blob(1048576) BINARY
Art 5 - Art 4 - Art 3
group_concat(name, '.', `type` order by `type` desc, name asc separator ' - ' limit 2, 6)
text / blob(1048576) BINARY
Art 4.Third - Art 7.Third - Art 2.Second - Art ...
group_concat(null)
text / blob(1048576) BINARY
NULL
The examples were created with the MyWAY SQL manager: Download

How to use the GROUP_CONCAT() function in MySQL and MariaDB databases

The GROUP_CONCAT function in MySQL and MariaDB is used to concatenate the values ​​of a column within a group of a SQL query into a single string. The function allows multiple values ​​to be combined into a single string with optional delimiters, and can be used for tasks such as aggregating data from multiple rows into a single string, creating comma-separated lists, and generating summary reports.

Concatenating values ​​into groups allows related values ​​to be combined in comma-separated lists or other desired formats. Furthermore, the keyword DISTINCT can be used to remove duplicate values ​​before concatenation, for example to exclude redundant values ​​in concatenated results. The order of the values ​​can also be determined and user-defined delimiters can be specified in concatenated results.
FROM_UNIXTIMEGET_FORMATGREATESTGROUP_CONCATHEXHOURIF

Further MySQL und MariaDB SQL Aggregate functions

ANY_VALUEANY_VALUE(Column)
More about ANY_VALUE Function

AVGAVG(Expression or column)
More about AVG Function

BIT_ANDBIT_AND(Expression or column)
More about BIT_AND Function

BIT_ORBIT_OR(Expression or column)
More about BIT_OR Function

BIT_XORBIT_XOR(Expression or column)
More about BIT_XOR Function

COUNTCOUNT(Expression or column)
More about COUNT Function

JSON_ARRAYAGGJSON_ARRAYAGG(Expression or column)

JSON_OBJECTAGGJSON_OBJECTAGG(Expression or column)

MAXMAX(Expression or column)
More about MAX Function

MINMIN(Expression or column)
More about MIN Function

SUMSUM(Expression or column)
More about SUM Function

STDSTD(Expression or column)
More about STD Function

STDDEVSTDDEV(Expression or column)
More about STDDEV Function

STDDEV_POPSTDDEV_POP(Expression or column)
More about STDDEV_POP Function

STDDEV_SAMPSTDDEV_SAMP(Expression or column)
More about STDDEV_SAMP Function

VAR_POPVAR_POP(Expression or column)
More about VAR_POP Function

VAR_SAMPVAR_SAMP(Expression or column)
More about VAR_SAMP Function

VARIANCEVARIANCE(Expression or column)
More about VARIANCE Function

Numeric functionsString functionsRegular ExpressionsDate and Time functions
Comparison functionsEncryption & CompressionConversion functionsNULL functions
Aggregate functionsWindow functionsJSON functionsGeometric functions
Sequence functionsInformation functionsDynamic ColumnsMiscellaneous functions
More information about the GROUP_CONCAT SQL function: mysql.com and mariadb.com
Updated: 2023-09-17ImprintContactTerms & conditionsPrivacy & CookiesUpdatesSitemapFacebookLinkedinTwitterStatistics©2020-2024 Heino Cunze-Fischer