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

吾爱主题 阅读:150 2024-04-05 16:21:46 评论:0

如图:

表数据

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

通过存储过程来写

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 //

创建如上存储过程

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

总结

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

原文链接:https://blog.csdn.net/xcc_2269861428/article/details/106529796

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容