mysql查询节点下的所有叶子节点

来源:互联网 发布:数控车设计图和编程 编辑:程序博客网 时间:2024/04/30 07:19

使用存储过程查询节点下的所有叶子节点:树的表结构:节点ID:t_sort_id,  父节点:t_sort_farther

procedure1:

begin
 declare deep int;
 set deep=1;
 drop TABLE IF EXISTS tmpLst;
 CREATE TABLE tmpLst(id int primary key,pid int,depth int );
 insert INTO tmpLst select t_sort_id,t_sort_farther,deep from tool_sort where t_sort_farther=PID;
 while row_count()>0 do
  set deep=deep+1;
  insert INTO tmpLst select a.t_sort_id,a.t_sort_farther,deep from tool_sort a left join tmpLst b on a. t_sort_farther=b.id where a.t_sort_id<>1 and b.depth=deep-1;
 end while;
 call procedure2;
 select * from tmpLst;
end

procedure2:

begin
 delete from tmpLst where id in (select * from(select pid from tmpLst)as a );         //不执行这句话就是查询了所有的子节点

end

不知道为什么procedure2中的delete这一句直接写在procedure1里面就不行,盼高手解答!

原创粉丝点击