GROUP_CONCAT() concatenates non-NULL values from a group into one string. It returns NULL when there are no non-NULL values.
ORDER BY inside GROUP_CONCAT(). The result length is also limited by the group_concat_max_len system variable (1024 bytes by default).SELECT GROUP_CONCAT(expression) AS combined_values
FROM table_name;
We will use our sale table with three columns, customer_id,product_id, quantity. Download the table structure with data at the end of this page.
| customer_id | product_id | quantity |
|---|---|---|
| C2 | P4 | 5 |
| C3 | P5 | 2 |
| C2 | P3 | 3 |
| ...... | ....... | ... |
SELECT customer_id, product_id, quantity
FROM plus2_sale
WHERE customer_id = 'C3';
| CUSTOMER_ID | product_id | quantity |
|---|---|---|
| C3 | P5 | 2 |
| C3 | P4 | 2 |
| C3 | P5 | 2 |
| C3 | P4 | 2 |
SELECT customer_id,
GROUP_CONCAT(product_id, ':', quantity) AS purchases
FROM plus2_sale
WHERE customer_id = 'C3'
GROUP BY customer_id;
| CUSTOMER_ID | GROUP_CONCAT(product_id,':',quantity) |
|---|---|
| C3 | P5:2,P4:2,P5:2,P4:2 |
SELECT customer_id,
GROUP_CONCAT(product_id, ':', quantity) AS purchases
FROM plus2_sale
GROUP BY customer_id;
| CUSTOMER_ID | GROUP_CONCAT(product_id,':',quantity) |
|---|---|
| C1 | P3:5,P3:5 |
| C2 | P4:5,P5:2,P3:3,P4:5,P4:6, P5:2,P3:3,P4:6 |
| C3 | P4:2,P5:2,P4:2,P5:2 |
SELECT customer_id,
GROUP_CONCAT(product_id, ':', quantity SEPARATOR '; ') AS purchases
FROM plus2_sale
GROUP BY customer_id;
| CUSTOMER_ID | GROUP_CONCAT(product_id, ':', quantity SEPARATOR '; ') |
|---|---|
| C1 | P3:5; P3:5 |
| C2 | P4:5; P5:2; P3:3; P4:5; P4:6; P5:2; P3:3; P4:6 |
| C3 | P4:2; P5:2; P4:2; P5:2 |
SELECT customer_id,
GROUP_CONCAT(DISTINCT product_id, ':', quantity SEPARATOR '; ') AS purchases
FROM plus2_sale
GROUP BY customer_id;
| CUSTOMER_ID | GROUP_CONCAT(DISTINCT product_id, ':', quantity SEPARATOR '; ') |
|---|---|
| C1 | P3:5 |
| C2 | P4:5; P5:2; P3:3; P4:6 |
| C3 | P4:2; P5:2 |
SELECT customer_id,
GROUP_CONCAT(product_id, '->', quantity ORDER BY quantity DESC) AS purchases
FROM plus2_sale
GROUP BY customer_id;
| CUSTOMER_ID | GROUP_CONCAT(product_id, '->', quantity order by quantity desc) |
|---|---|
| C1 | P3->5,P3->5 |
| C2 | P4->6,P4->6,P4->5,P4->5,P3->3,P3->3, P5->2,P5->2 |
| C3 | P5->2,P4->2,P5->2,P4->2 |
SELECT customer_id,
GROUP_CONCAT(product_id, SUM(quantity))
FROM plus2_sale
GROUP BY customer_id, product_id;
#1111 - Invalid use of group functionSELECT customer_id,
GROUP_CONCAT(product_id, ':', quantity_sum SEPARATOR ' ; ') AS purchases
FROM (
SELECT customer_id, product_id, SUM(quantity) AS quantity_sum
FROM plus2_sale
GROUP BY customer_id, product_id
) AS sale_totals
GROUP BY customer_id;
| customer_id | GROUP_CONCAT(product_id,':', cast(quantity_sum as char) SEPARATOR ' ; ' ) |
|---|---|
| C1 | P3:10 |
| C2 | P3:6 ; P4:22 ; P5:4 |
| C3 | P5:4 ; P4:4 |
SELECT customer_id,
GROUP_CONCAT(product_id, ':', CAST(quantity_sum AS CHAR) SEPARATOR ' ; ') AS purchases
FROM (
SELECT customer_id, product_id, SUM(quantity) AS quantity_sum
FROM plus2_sale
GROUP BY customer_id, product_id
) AS sale_totals
GROUP BY customer_id;
Use this small InnoDB/utf8mb4 table to reproduce the examples.
CREATE TABLE IF NOT EXISTS plus2_sale (
customer_id VARCHAR(3) NOT NULL,
product_id VARCHAR(3) NOT NULL,
quantity INT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- Sample data
INSERT INTO plus2_sale (customer_id, product_id, quantity) VALUES
('C2', 'P4', 5),
('C3', 'P5', 2),
('C2', 'P3', 3),
('C2', 'P5', 2),
('C3', 'P4', 2),
('C1', 'P3', 5),
('C2', 'P4', 6),
('C2', 'P4', 5),
('C3', 'P5', 2),
('C2', 'P3', 3),
('C2', 'P5', 2),
('C3', 'P4', 2),
('C1', 'P3', 5),
('C2', 'P4', 6);
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.