当前位置: 移动技术网 > IT编程>数据库>Mysql > mysql 使用存储过程实现树节点的获取方法

mysql 使用存储过程实现树节点的获取方法

2020年06月23日  | 移动技术网IT编程  | 我要评论

如图:

表数据

这样的一棵树,如何获取“高寅瑞”下的所有节点(一条sql语句是肯定搞不定的)

通过存储过程来写

delimiter //
create function `getchildlst`(rootid int)
 
returns varchar(1000) reads sql data
 
begin
 declare stemp varchar(1000);
 
 declare stempchd varchar(1000);
 
 set stemp = '$';
 
 set stempchd =cast(rootid as char);
 
 while stempchd is not null do
 
 set stemp = concat(stemp,',',stempchd);
 
 select group_concat(id) into stempchd from document_file_name where find_in_set(pid,stempchd)>0;
 
 end while;
 
 return stemp;
 
end //

创建如上存储过程

select * from document_file_name where find_in_set(id, getchildlst(1));

总结

到此这篇关于mysql 使用存储过程实现树节点的获取的文章就介绍到这了,更多相关mysql 存储过程树节点获取内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网