GROUP_CONCAT useful GROUP BY extension
MySQL has useful extention to the GROUP BY operation: function GROUP_CONCAT:
GROUP_CONCAT(expr) - This function returns a string result with the concatenated non-NULL values from a group.
Where it can be useful?
For example to get PHP array without looping inside PHP:
Table:
-
CREATE TABLE services (
-
id INT UNSIGNED NOT NULL,
-
client_id INT UNSIGNED NOT NULL,
-
KEY (id));
-
INSERT INTO services
-
VALUES (1,1),(1,2),(3,5),(3,6),(3,7);
-
-
SELECT id,client_id FROM services WHERE id = 3;
-
+----+-----------+
-
| id | client_id |
-
+----+-----------+
-
| 3 | 5 |
-
| 3 | 6 |
-
| 3 | 7 |
-
+----+-----------+
-
-
SELECT id,GROUP_CONCAT(client_id) FROM services WHERE id = 3 GROUP BY id;
-
+----+-------------------------+
-
| id | GROUP_CONCAT(client_id) |
-
+----+-------------------------+
-
| 3 | 5,6,7 |
-
+----+-------------------------+
Handling in PHP:
old way:
-
<?php
-
$res=$mysqli->query("SELECT id,client_id FROM services WHERE id = 3");
-
while ($row = $res->fetch_array(MYSQLI_ASSOC)) {
-
$result[] = $row['client_id'];
-
}
-
$res->free();
-
?>
with group_concat:
-
<?php
-
$res=$mysqli->query("SELECT id,GROUP_CONCAT(client_id) as clients FROM services WHERE id = 3 GROUP BY id");
-
$row = $res->fetch_array(MYSQLI_ASSOC);
-
$result = explode(',', $row['clients']); // $row['clients'] contains string 5,6,7
-
$res->free();
-
?>
This should work faster, as we remove loop from PHP to MySQL server side.
Also it can be handy to use result concatenated string as part of IN statement:
-
<?php
-
$res=$mysqli->query("SELECT id,GROUP_CONCAT(client_id) as clients FROM services WHERE id = 3 GROUP BY id");
-
$row = $res->fetch_array(MYSQLI_ASSOC);
-
$result = $row['clients']; // $row['clients'] contains string 5,6,7
-
$res->free();
-
-
$resclients=$mysqli->query("SELECT id,client_name FROM clients WHERE id = IN ($result)");
-
// handle $resclients
-
-
?>
Sure, last example can be handled with one query with joins, but sometimes we need the temporary ids in clients code, for example to execute query on another server.
One more thing: you may want to add ORDER BY NULL statement after GROUP_BY to avoid
unnecessary sorting with filesort
17 Comments











del.icio.us
digg
GROUP_CONCAT in MySQL…
GROUP_CONCAT(expr) - This function returns a string result with the concatenated non-NULL values from a group.
Where it can be useful?
For example to get PHP array without looping inside PHP:
CREATE TABLE services (
id INT UNSIGNED NOT NULL,
client_id …
Trackback :: September 6, 2006 @ 3:52 am
[...] GROUP_CONCAT useful GROUP BY extension: MySQL has useful extension to the GROUP BY operation: function GROUP_CONCAT: GROUP_CONCAT(expr) - This function returns a string result with the concatenated non-NULL values from a group. [...]
Pingback :: September 7, 2006 @ 12:44 pm
Nice info
Comment :: October 12, 2006 @ 1:44 am
there is error in line 4 in example 3
Comment :: January 27, 2007 @ 7:31 am
Vladimir
Thank you, fixed.
Comment :: January 31, 2007 @ 12:28 pm
A litte problem I had with the group_concat function was when selecting integers only I was getting a blob instead of a string to solve this I used a cast:
SELECT GROUP_CONCAT(CAST(myInt as CHAR)) myInts FROM aTable;
Comment :: June 8, 2007 @ 8:49 am
[...] MySQL Performance Blog [...]
Pingback :: August 23, 2007 @ 1:10 am
Hi, Thanks
Do anybody know how to do the same as the GROUP_CONCAT() function do
in MS-Access? It seems don’t have this kind of functions
Comment :: January 24, 2008 @ 9:32 am
My sincere thanks to - 6. paul carey for publishing this fix - you saved my sanity.
Comment :: January 30, 2008 @ 11:19 am
I have created a table (with only 2 fields) with the following query
1.CREATE TABLE `users` (
2.`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
3.`name` VARCHAR( 20 ) NOT NULL
4.) ENGINE = MYISAM ;”
There are 20,000 users in this table with ids from 1 to 20,000
On executing the following query
Code: ( text )
1. select group_concat(id separator ‘,’) from users
returns only 283 ids separated with ‘,’
Comment :: May 15, 2008 @ 12:28 am
GROUP_CONCAT IS VERY NICE, BUT THERE IS A LIMIT UPTO 1024, CAN WE INCREASE THE LIMIT? IF YES PLEASE
Comment :: June 5, 2008 @ 11:26 pm
There is group_concat_max_len server variable, which by default is 1024
Comment :: June 6, 2008 @ 3:43 pm
how to extent the limit of group concat > 1024
Comment :: June 10, 2008 @ 8:52 pm
group_concat_max_len=4096 in my.cnf
or SET GLOBAL group_concat_max_len=4096
Comment :: June 11, 2008 @ 10:16 am
where i can get my.cnf file or
how to set global
Comment :: June 12, 2008 @ 12:50 am
Hi, is it possible to use some kind of argument in group_concat that allows it to group 2 by 2? p.ex:
SELECT id,client_id FROM services WHERE id = 3;
+—-+———–+
| id | client_id |
+—-+———–+
| 3 | 5 |
| 3 | 6 |
| 3 | 7 |
| 3 | 8 |
| 3 | 9 |
| 3 | 10 |
| 3 | 11 |
+—-+———–+
SELECT id,GROUP_CONCAT(client_id,2) FROM services WHERE id = 3 GROUP BY id;
+—-+————————-+
| id | GROUP_CONCAT(client_id) |
+—-+————————-+
| 3 | 5,6 |
+—-+————————-+
| 3 | 6,7 |
+—-+————————-+
| 3 | 7,8 |
+—-+————————-+
| 3 | 8,9 |
+—-+————————-+
| 3 | 9,10 |
+—-+————————-+
| 3 | 10,11 |
+—-+————————-+
Thanks!
Comment :: June 30, 2008 @ 7:48 am
Has MySQL rollup, cubes or grouping sets like Oracle?
Comment :: July 3, 2008 @ 2:30 am