Tuesday, November 18, 2008

GROUP_CONCAT in MySQL

I had the scenario for the table like below.

Table: test

id title
_________________

1 T2
1 T3
1 T1
2 L2
2 L1

I want the results like

id title
_________________

1 T2,T3,T1
2 L2,L1


For this, SQL would be

select id,GROUP_CONCAT('title') from test GROUP BY id ORDER by id


Now i want the values of title to be sorted.

This can be done using the below query

select id,GROUP_CONCAT('title' ORDER BY title) from test GROUP BY id ORDER by id

The results would be

id title
_________________

1 T1,T2,T3
2 L1,L2


No comments: