0

I want to fetch all the child ids (comma separeted) but my code just getting the two level heirachy, not more than that. I dont know whats issue? here is my code

 select concat (a.id,',',a.parent_id) as parents
  from table1 a 
 inner join  table1 b 
    on a.id = b.parent_id
 where b.id = 5

Here is my table structure:

    id | parent_id
    ---------------------
     1 |      6
     2 |      NULL
     3 |         1
     4 |         3
     5 |         4
     6 |      NULL
     7 |      NULL

output expected:

4,3,1,6
Maha Dev
  • 3,915
  • 2
  • 31
  • 50

1 Answers1

0

Try

SELECT GROUP_CONCAT(@temp:=T.parent_id) as parents
FROM (SELECT * FROM testTable ORDER BY id DESC) T
 JOIN
 (SELECT @temp:=5)tmp
WHERE T.id=@temp;

Refer : SQL Fiddle

zakhefron
  • 1,403
  • 1
  • 9
  • 13