Sunday 30 June 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...