-- 构造测试数据2 CREATE TABLE `treetable` ( `id` int NOT NULL , `name` varchar(50) NOT NULL , `parid` int NOT NULL , PRIMARY KEY (`id`) ) ; insert into treetable select 1,'1',0 union all select 2,'1-1',1 union all select 3,'1-1-1',2 union all select 4,'1-1-1-1',3 union all select 5,'1-1-1-1-1',4
-- 递归找儿子 select * from (select id,name,if(parid = @a,@a := id,0) as hassum from treetable,(select @a := 2) as t1 order by parid ) as t where t.hassum !=0;