-- 构造测试数据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',
-- 递归找父亲 select * from (select id,name,if(id = (select parid from treetable where id= @a),@a := id,0) as hassum from treetable,(select @a := 4) as t1 order by parid desc ) as t where t.hassum !=0;
这篇博客探讨了如何在数据库中进行递归查询以查找父级节点。通过创建一个名为`treetable`的表并插入数据,然后使用一个递归SQL查询来寻找指定ID的父节点。这个查询方法对于处理层次结构数据非常有用,特别是在需要追溯层级关系的场景下。
7171

被折叠的 条评论
为什么被折叠?



