0

I have two tables

table1

|---------------------|------------------|------------------|
|      student_id     |   studentname    |     parentid     |
|---------------------|------------------|------------------|
|          1          |       Kobe       |        1         |
|---------------------|------------------|------------------|
|---------------------|------------------|------------------|
|          2          |      Lebron      |        1         |
|---------------------|------------------|------------------|


table2
|---------------------|------------------|
|       parentid      |    parentname    |   
|---------------------|------------------|
|          1          |      Jordan      |
|---------------------|------------------|

I want to select an output like this

result
|---------------------|------------------|------------------|---------------- 
--
|       parentid      |    parentname    |    childrenid    |  childrenname
|---------------------|------------------|------------------|------------------
|          1          |      Jordan      |     1,2          |  Kobe,Jordan
|---------------------|------------------|------------------|------------------

is this possible to happen? thanks in advance I already use this query but the rows is duplicating

select * from table2 left join table1 ON table1.parentid = table2.parentid

2 Answers2

0

No it is not possible, you have to use left join which will result in two rows one for Kobe and other for Lebron.

Roshaan Farrukh
  • 399
  • 3
  • 10
0

use GROUP_CONCAT funtion in mysql

select parentname,
GROUP_CONCAT(student_id SEPARATOR ',') AS childrenid,
GROUP_CONCAT(studentname SEPARATOR ',') childrenname  
from table2 p left join table1 s on
p.parentid=s.parentid
group by parentname

parentname  childrenid  childrenname
Jordan       1,2        Kobe,Lebron

http://www.sqlfiddle.com/#!9/4d9127/2

Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63