2

Possible Duplicate:
Can I concatenate multiple MySQL rows into one field?

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?

Community
  • 1
  • 1
Antonio Papa
  • 1,596
  • 3
  • 20
  • 38

3 Answers3

7
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

4

Try GROUP_CONCAT()

SELECT item, GROUP_CONCAT(size)
FROM table_name
GROUP BY item
Moyed Ansari
  • 8,436
  • 2
  • 36
  • 57
4

Query:

SQLFIDDLEEXample

SELECT item,
       group_concat(size separator ', ') AS SIZE
FROM Table1
GROUP BY item

Result:

|  ITEM |     SIZE |
--------------------
| dress | S, L, XL |
| shirt |    L, XL |
Justin
  • 9,634
  • 6
  • 35
  • 47