Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql query to list all the items in a group in one record

Tags:

sql

mysql

I need to write a query that will return to me all the items in a group in one record, separated by commas,from two tables, example result below,

Items table:

--------------------
Name    |  Group_ID
--------------------
item1   |  1
item2   |  1
item3   |  3    

Group table:

--------------------
ID    |  Name
--------------------
 1    |  Group1
 3    |  Group3     

Result i'm looking for:

------------------------------
GId  |  Items  
------------------------------
 1   |  item1, item2
 3   |  item3      
like image 975
LaggKing Avatar asked Aug 28 '13 19:08

LaggKing


1 Answers

USE GROUP_CONCAT

SELECT group_concat(Name) FROM table
like image 118
aleroot Avatar answered Sep 21 '22 00:09

aleroot