Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

joing the rows into together using mysql

Tags:

php

mysql

I want to join multiple row values from table.

Table name:Item

    ID |            item_id |     Value
    1               43          item1
    2               44          item2
    3               44          item3
    4               44          item4
    5               45          item5
    6               45          item6

ID's are unique (primary key) What I am looking to output is a mysql query is something to which gives this output as given below

Output:

  ID |            item_id |     Value
    1               43          item1
    2               44          item2,item3,item4
    3               44          item2,item3,item4
    4               44          item2,item3,item4
    5               45          item5,item6
    6               45          item5,item6

kindly requesting to give some suggestions

like image 607
user092 Avatar asked Oct 19 '22 14:10

user092


2 Answers

Try this query:

SELECT t1.ID, t1.item_id, t2.Value
FROM item t1
INNER JOIN
(
    SELECT item_id, GROUP_CONCAT(Value) AS Value
    FROM item
    GROUP BY item_id
) t2
    ON t1.item_id = t2.item_id

Follow the link below for a running demo:

SQLFiddle

like image 103
Tim Biegeleisen Avatar answered Oct 21 '22 06:10

Tim Biegeleisen


You can use below query.

SELECT GROUP_CONCAT(value) FROM Item GROUP BY item_id;
like image 32
RJParikh Avatar answered Oct 21 '22 04:10

RJParikh