Sunday, June 30, 2019

mysql - SQL data in one field separated by coma










I have three tables items, sizes and item_sizes for many to many relationship with join i can query this:




item     size
shirt L
shirt XL
dress S
dress L
dress XL


But i want this:




item     size
shirt L, XL
dress S, L, XL


Speed doesn't matter i want only the results.I can do it with while loop but is there another way of doing this query?


Answer



select item, group_concat(size)
from the_table
group by item;



More details in the manual: http://dev.mysql.com/doc/refman/5.5/en/group-by-functions.html#function_group-concat


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...