0

I want to select "Apple" then output "John" and "Michelle". I try to run the code but it is not working. Please help.

select @start := title as 'title', id, parent
from mytable
join 
(select @start := 0) temp
where parent = @start and title = "Apple";

Link : http://sqlfiddle.com/#!9/91fd40/1/0

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
zac1987
  • 2,721
  • 9
  • 45
  • 61

1 Answers1

0

I found the answer myself just in case anyone need it Demo :

SELECT T2.id, T2.title, T2.parent
FROM (
    SELECT
        @r AS _id,
        (SELECT @r := parent FROM mytable WHERE id = _id) AS parent,
        @l := @l + 1 AS lvl
    FROM
        (SELECT @r := 6, @l := 0) vars,
        mytable h
    WHERE @r <> 0) T1
JOIN mytable T2
ON T1._id = T2.id 
where T2.title<>'sky'
ORDER BY T1.lvl DESC
zac1987
  • 2,721
  • 9
  • 45
  • 61