ashwin kumar ashwin kumar - 1 year ago 85
SQL Question

Sql Merging multiple records into one record

I have table with two columns user_id and tags.

user_id tags
1 <tag1><tag4>
1 <tag1><tag2>
1 <tag3><tag2>
2 <tag1><tag2>
2 <tag4><tag5>
3 <tag4><tag1>
3 <tag4><tag1>
4 <tag1><tag2>

I want to merge this two records into one record like this.

user_id tags
1 tag1, tag2, tag3, tag4
2 tags, tag2, tag4, tag5
3 tag4, tag1
4 tag1, tag2

How can i get this? Can anyone help me out.
Also need to convert tags field into array [].
I don't have much knowledge on typical sql commads. I just know the basics. I am a ruby on rails guy.

Answer Source

You should look into the GROUP_CONCAT function in mysql.A good example is here

In your case it would be something like:

SELECT user_id, GROUP_CONCAT(tags) FROM tablename GROUP BY user_id
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download