MySQL关于递归的一个问题
技术百科
黄舟
发布时间:2017-05-21
浏览: 次 这篇文章主要介绍了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 | pa
rentid |
+--------------------------------------+------+----------+
| 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 | +------+----------+------+-------+*/
# 的是
# 这篇文章
# 可通过
# 不支持
# 递归
# 中对
# mysql
# 给个
# 中不
# 中学教师
# 高数
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- php与c语言在嵌入式中有何区别_对比两者在硬件控
- Win11搜索栏无法输入_解决Win11开始菜单搜
- VSC怎样用终端运行PHP_命令行执行脚本的步骤【
- 如何使用Golang安装依赖库_管理模块和第三方包
- Win11怎么制作U盘启动盘_Win11原版系统安
- c++ namespace命名空间用法_c++避免
- Win11怎么设置鼠标宏_Win11鼠标按键自定义
- 如何使用正则表达式提取以编号开头、后跟多个注解的完
- Win11怎么开启游戏模式_Windows11优化
- php打包exe怎么传递参数_命令行参数接收方法【
- 如何在Golang中实现邮件发送功能_Golang
- 如何在Golang中使用time处理时间_Gola
- Win11怎样安装剪映专业版_Win11安装剪映教
- Win11怎么修复系统文件_使用sfc命令修复Wi
- 如何在 Go 应用中实现自动错误恢复与进程重启机制
- Python大文件处理策略_内存优化说明【指导】
- VSC里PHP变量未定义报错怎么解决_错误抑制技巧
- Windows如何使用注册表查找和删除项?(reg
- 如何从 Go 的 map[string]inter
- 如何在Golang中使用replace替换模块_指
- 如何使用Golang匿名函数_快速定义临时函数逻辑
- Windows10蓝屏代码DPC_WATCHDOG
- 微信企业付款回调PHP怎么接收_处理企业付款异步通
- Win11怎么设置多显示器任务栏 Win11扩展任
- 如何使用Golang写入二进制文件_Golang
- php做exe支持多线程吗_并发处理实现方式【详解
- Win11局域网共享怎么设置 Win11文件夹网络
- Python邮件系统自动化教程_批量发送解析与模板
- Win11笔记本怎么看电池健康度_Win11电池报
- Win11任务栏天气怎么关闭 Win11隐藏天气小
- Windows怎样关闭开始菜单推荐广告_Windo
- 新手学PHP架构总混淆概念咋办_重点梳理【教程】
- 短链接怎么用php还原_从基础原理到代码实现教学【
- Golang如何测试HTTP中间件_Golang
- php中::能用于接口静态方法吗_接口静态方法调用
- Win11怎么设置按流量计费_Win11限制后台流
- Windows10如何查看保存的WiFi密码_Wi
- Go 中 defer 语句在 goroutine
- 如何用::实现工具类方法调用_php静态工具类设计
- PythonPandas数据分析教程_数据清洗与处
- PHP主流架构怎么集成Redis缓存_配置步骤【方
- 如何在Golang中使用闭包_封装变量与函数作用域
- Windows如何设置登录时的欢迎屏幕背景?(锁屏
- Win11怎么开启游戏模式_Win11优化游戏帧数
- Win11怎么查看显卡温度 Win11任务管理器查
- Windows10电脑怎么设置虚拟内存_Win10
- Python网络异常模拟_测试说明【指导】
- Linux怎么查找死循环进程_Linux系统负载分
- Win11怎么关闭搜索历史_Win11清除任务栏搜
- c++如何打印函数堆栈信息_c++ backtra

rentid |
+--------------------------------------+------+----------+
| 37e2ea0a-1c31-3412-455a-5e60b8395f7d | 教师 | NULL |
+--------------------------------------+------+----------+
call usp_ser('37e2ea0a-1c31-3412-455a-5e60b8395f7d');
Empty set (0.02 sec)
QQ客服