服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - mysql 使用存储过程实现树节点的获取方法

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

2021-01-21 16:32前方太黑暗 Mysql

这篇文章主要介绍了mysql 使用存储过程实现树节点的获取方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

如图:

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

表数据

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

这样的一棵树,如何获取“高寅瑞”下的所有节点(一条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 使用存储过程实现树节点的获取的文章就介绍到这了,更多相关mysql 存储过程树节点获取内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

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

延伸 · 阅读

精彩推荐