登录  /  注册

MySQL之递归小问题实例分享

小云云
发布: 2018-01-24 13:39:41
原创
1309人浏览过

mysql本身不支持递归语法,但可通过自连接变相实现一些简单的递归,本文主要介绍了mysql之递归小问题,需要的朋友可以参考下,希望能帮助到大家。


--递归小方法:临时表和普通表的不同方法
--这题使用的是2次临时表查询父节点的递归 
drop table if exists test;
create table test(
id varchar(100),
name varchar(20),
parentid varchar(100)
);
insert test select
'13ed38f1-3c24-dd81-492f-673686dff0f3', '大学教师', '37e2ea0a-1c31-3412-455a-5e60b8395f7d' union all select 
'1ce203ac-ee34-b902-6c10-c806f0f52876','小学教师', '37e2ea0a-1c31-3412-455a-5e60b8395f7d' union all select 
'37e2ea0a-1c31-3412-455a-5e60b8395f7d', '教师' ,      null                union all select 
'c877b7ea-4ed3-f472-9527-53e1618cb1dc', '高数老师', '13ed38f1-3c24-dd81-492f-673686dff0f3' union all select 
'ce50a471-2955-00fa-2fb7-198f6b45b1bd', '中学教师', '37e2ea0a-1c31-3412-455a-5e60b8395f7d';
 
delimiter $$
 
create procedure usp_ser(in idd varchar(100))
begin
declare lev int;
set lev=1;
drop table if exists tmp1;
drop table if exists tmp2;
CREATE TEMPORARY TABLE tmp1(id varchar(100),name varchar(20),parentid varchar(100),levv int);
CREATE TEMPORARY TABLE tmp2(pid varchar(100));
insert tmp2 select parentid from test where id=idd;
insert tmp1 select t.* , lev from test t join tmp2 a on t.id=a.pid;
    while exists(select 1 from tmp2 )
do
truncate tmp2;
set lev=lev+1;
insert tmp2 select t.id from test t join tmp1 a on t.id=a.parentid and a.levv=lev-1;
insert tmp1 select t.*,lev from test t join tmp2 a on t.id=a.pid;
end while ;
select id,name,parentid from tmp1;
end;
$$
 
delimiter ;
 
 call usp_ser('c877b7ea-4ed3-f472-9527-53e1618cb1dc');
+--------------------------------------+----------+--------------------------------------+
| id                  | name   | parentid               |
+--------------------------------------+----------+--------------------------------------+
| 13ed38f1-3c24-dd81-492f-673686dff0f3 | 大学教师 | 37e2ea0a-1c31-3412-455a-5e60b8395f7d |
| 37e2ea0a-1c31-3412-455a-5e60b8395f7d | 教师   | NULL                 |
+--------------------------------------+----------+--------------------------------------+
 
 call usp_ser('13ed38f1-3c24-dd81-492f-673686dff0f3');
+--------------------------------------+------+----------+
| id                  | name | parentid |
+--------------------------------------+------+----------+
| 37e2ea0a-1c31-3412-455a-5e60b8395f7d | 教师 | NULL   |
+--------------------------------------+------+----------+
 
 call usp_ser('37e2ea0a-1c31-3412-455a-5e60b8395f7d');
 
Empty set (0.02 sec)
登录后复制

上面的方法因为由于MySQL中不允许在同一语句中对临时表多次引用,所以用2次临时表
下面给个一次性用普通表完成的 查询子节点的递归查询

核心代码


drop table if exists test;
create table test(
id INT,
parentid INT
);
insert test select
1, 0 UNION ALL SELECT 
2, 1 UNION ALL SELECT 
3, 1 UNION ALL SELECT 
4, 0 UNION ALL SELECT 
5, 2 UNION ALL SELECT 
6, 5 UNION ALL SELECT 
7, 3 ;
Go
 
delimiter $$
 
create procedure usp_ser(in idd varchar(100))
begin
declare lev int;
set lev=1;
drop table if exists tmp1;
CREATE TABLE tmp1(id INT,parentid INT ,levv INT,ppath VARCHAR(1000));

INSERT tmp1 SELECT *,lev,id FROM test WHERE parentid=idd;

 while row_count()>0
do

set lev=lev+1;
insert tmp1 select t.*,lev,concat(a.ppath,t.id) from test t join tmp1 a on t.parentid=a.id AND levv=LEV-1;
 
end while ;
SELECT * FROM tmp1;
 
end;
$$
 
delimiter ;
 
 call usp_ser(0);
 
/*
+------+----------+------+-------+
| id  | parentid | levv | ppath |
+------+----------+------+-------+
|  1 |    0 |  1 | 1   |
|  4 |    0 |  1 | 4   |
|  2 |    1 |  2 | 12  |
|  3 |    1 |  2 | 13  |
|  5 |    2 |  3 | 125  |
|  7 |    3 |  3 | 137  |
|  6 |    5 |  4 | 1256 |
+------+----------+------+-------+*/
登录后复制

相关推荐:

详解PHP通过递归实现提成计算

JavaScript中递归函数的问题解决

递归调用函数方法

以上就是MySQL之递归小问题实例分享的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号