MySQL GROUP_CONCAT Function with Examples

How to Use the MySQL GROUP_CONCAT Function

In this tutorial, you will learn how to use the MySQL GROUP_CONCAT function, which returns a string with concatenated values from a group and returns NULL if there are no non-NULL values.

Learn more:

MySQL GROUP_CONCAT Syntax

The syntax of the GROUP_CONCAT function is as follows:

GROUP_CONCAT([DISTINCT] expression

[ORDER BY expression] [SEPARATOR string_value]);

Remarks

  • If you want to eliminate duplicate values, use the DISTINCT clause.
  • To sort the values in the result in ascending or descending order, use the ORDER BY clause.
  • The default separator between values in a group is a comma (,). If you need to specify it explicitly, use SEPARATOR, followed by the string value. However, if you need to eliminate the separator, use SEPARATOR ("")

GROUP_CONCAT Examples

The following is a table of orders:

order_idcustomer_codecustomer_nameorder_numberorder_dateamountsalesperson
1#CID_10001Customer A#ORD_100012019-03-011890.50Employee A
2#CID_10001Customer A#ORD_100022019-03-012300.00Employee A
3#CID_10003Customer C#ORD_100032019-03-011850.50Employee B
4#CID_10004Customer D#ORD_100042019-03-021240.50Employee C
5#CID_10004Customer D#ORD_100052019-03-022200.00Employee C
6#CID_10006Customer F#ORD_100062019-03-033200.80Employee A
7#CID_10007Customer G#ORD_100072019-03-041280.00Employee B
8#CID_10008Customer H#ORD_100082019-03-04980.00Employee C

Example 1: Return a comma (,) separated concatenated string

The following statement returns a comma (,) separated concatenated string of the results of the order_number column of the orders table:

SELECT GROUP_CONCAT(o.order_number ORDER BY o.order_number DESC)

FROM orders o;

Result:

ORD_10008,#ORD_10007,#ORD_10006,#ORD_10005,#ORD_10004,#ORD_10003,#ORD_10002,#ORD_10001

Here, the SEPARATOR clause is not specified, so the default separator, a comma, is used between the values in the group.

Example 2: Return a vertical bar (|) separated concatenated string

The following statement returns the result of concatenating the order_number columns of the orders table, separated by vertical bars (|):

SELECT GROUP_CONCAT(o.order_number ORDER BY o.order_number DESC SEPARATOR " | ")

FROM orders o;

Here, as you can see, the SEPARATOR clause specified (|), the vertical bar separator is used between values in a group.

Result:

ORD_10008 | #ORD_10007 | #ORD_10006 | #ORD_10005 | #ORD_10004 | #ORD_10003 | #ORD_10002 | #ORD_10001

Example 3: Return a list of a vertical bar (|) separated concatenated strings (1)

The following statement returns a list of a vertical bar (|) separated, concatenated string result of the order_number column for each group of a salesperson from the orders table:

SELECT o.salesperson, GROUP_CONCAT(o.order_number ORDER BY o.order_number DESC SEPARATOR " | ")

FROM orders o

GROUP BY o.salesperson;

Result:

salespersonResult1
Employee AORD_10006 | #ORD_10002 | #ORD_10001
Employee BORD_10007 | #ORD_10003
Employee CORD_10008 | #ORD_10005 | #ORD_10004

Example 4: Return a list of a vertical bar (|) separated concatenated strings (2)

The following statement returns a list of a vertical bar (|) separated, concatenated string result of the customer_name column for each group of a salesperson from the orders table:

SELECT o.salesperson, GROUP_CONCAT(o.customer_name ORDER BY o.customer_name ASC SEPARATOR " | ") AS Result1

FROM orders o

GROUP BY o.salesperson;

Result:

salespersonResult1
Employee ACustomer A | Customer A | Customer F
Employee BCustomer C | Customer G
Employee CCustomer D | Customer D | Customer H

Example 5: Return a list of a vertical bar (|) separated concatenated strings (distinct values)

The following statement returns a list of a vertical bar (|) separated, concatenated string result of the customer_name for each group of a salesperson from the orders table:

SELECT o.salesperson, GROUP_CONCAT(DISTINCT o.customer_name ORDER BY o.customer_name ASC SEPARATOR " | ") AS Result1

FROM orders o

GROUP BY o.salesperson;

Result:

salespersonResult1
Employee ACustomer A | Customer F
Employee BCustomer C | Customer G
Employee CCustomer D | Customer H

Here, the DISTINCT clause is used so that duplicate values are removed from the results for each group of salespeople.

Summary

In this tutorial, you have learned how to return a concatenated string using the MySQL GROUP_CONCAT function.


See also:
MySQL LIKE Operator Pattern Matching and Examples
MySQL SUBSTRING_INDEX Function with Examples
MySQL EXISTS Operator with Examples
MySQL ROW_NUMBER Function with Examples
MySQL CONCAT() Function | Concatenate Strings in MySQL

Leave a Comment