I have following table.
+-------+--------+-------+
|     Id|    Name| Parent|
+-------+--------+-------+
|      0|       A|     -1|
|      1|       B|      0|
|      2|       C|      1|
|      3|       D|      2|
|      4|       E|      2|
|      5|       F|      2|
|      6|       G|      2|
|      7|       H|      2|
|      8|       I|      2|
+-------+--------+-------+
-1 as parent means it has no parent. The hierarchy can go upto 7-8 levels.
I need to get all the children (direct + indirect). I tried something like:
SELECT Id, NAME, @pv:=Id AS 'Parent' FROM tbl
JOIN
(SELECT @pv:=-1)tmp
WHERE Parent=@pv
But it does not work.