mysql - Query to get children records of a parent -
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
parent means has no parent. hierarchy can go upto 7-8 levels. need children (direct + indirect). tried like:
select id, name, @pv:=id 'parent' tbl join (select @pv:=-1)tmp parent=@pv
but not work.
try query
select t1.name lev1, t2.name lev2 tbl1 t1 left join tbl1 t2 on t2.parent = t1.id t1.id = 2;
expected output
+-------+--------+ | lev1| lev2| +-------+--------+ | c| d| | c| e| | c| f| | c| g| | c| h| | c| i| +-------+--------+
Comments
Post a Comment