GROUP_CONCAT() function is used to concatenate column values into a single string.
Example:
Table ‘abc’ has following columns:
id customer_id
3 14
3 15
3 [...]
Post your trick
MySQL – The GROUP_CONCAT() function
2 Responses to “MySQL – The GROUP_CONCAT() function”
Leave a Reply
Post A Trick !
How does "Kodetricks" work?
We at kodeplay like to share knowledge. With Kodetricks, even you can join us. All you need to do is post a programming related trick if you have one or rate a trick if you like someone else's.
hey dharmesh,
This is a good mysql function for concating the rows.
I have a small doubt on your query you have mentioned above
SELECT id,GROUP_CONCAT(customer_id) FROM abc WHERE id = 3 GROUP BY id;
Here in the query you are giving where clause on id which will bring rows having id =3. Then is there any need of giving GROUP BY clause??????
OR for using GROUP_CONCAT function GROUP BY clause is necesssary?????
Thanx jimit,
Well the use of where clause is optional,depending on your requirements.
when you are wrting only GROUP BY clause, then you will get the following output :
id customer_id
3 14,15,16
4 17,18
5 19,20 and so on
but if you want a specific row then you have to use WHERE clause.
the above query was:
select id, GROUP_CONCAT(customer_id) FROM abc GROUP BY id
It is not necessary to use group by clause always with group_concat.