Reputation: 570
I have a MySQL table StudentName(id,name) that looks like this:
id | name
++++++++
1 | alex
1 | adam
1 | adnan
2 | ben
2 | bush
3 | cris
4 | daisi
4 | diana
And I'd like to make a new table like this:
id | name
+++++++++++
1 | alex, adam, adnan
2 | ben, bush
3 | cris
4 | daisi, diana
Is there a way to accomplish this?
Upvotes: 5
Views: 10702
Reputation: 311088
The group_concat
function is what you're looking for:
SELECT id, GROUP_CONCAT(name ORDER BY name ASC SEPARATOR ', ')
FROM my_table
GROUP BY id
Upvotes: 22