group_concat() – Mysql
This function allows you to concat multiple rows in to a single string based on value.
This is the query for that -
select group_concat(tag separator ‘,’) as output from design_tag group by design_id
Real world problem and Scenario:
If you have 2 tables: design and design_tag, look at structure of the tables.
design_tag table structure:
If you want to store the “design_tag.tag” in to “design.tags” with comma separator based on the relation of design.id=design_tag.design_id then use this query.
update design set tags=(select group_concat(tag separator ‘,’) from design_tag where id=design_id group by design_id)
This will produce the final output like this:
Learn more about group_concat here:
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
RSS Feed
Twitter
September 13th, 2011
Prashant Patil 



