MySQL之递归小问题实例分享
技术百科
小云云
发布时间:2018-01-24
浏览: 次 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-5e60b83
95f7d');
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
# 给个
# 中不
# 中学教师
# 高数
相关栏目:
<?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; ?>
】
相关推荐
- c++怎么使用类型萃取type_traits_c+
- Win11怎么关闭自动更新 Win11永久关闭系统
- PythonPandas数据分析项目教程_时间序列
- Python与OpenAI接口集成实战_生成式AI
- 如何用::实现单例模式_php静态方法与作用域操作
- Win11怎么设置闹钟_Windows 11时钟应
- Python对象比较与排序_集合使用说明【指导】
- How to Properly Use NumPy
- Mac如何备份到iCloud_Mac桌面与文稿文件
- Win11怎么清理C盘虚拟内存_Win11清理虚拟
- Drupal 中 HTML 链接被重复转义导致渲染
- Windows服务持续崩溃怎样修复_系统服务保护机
- Win10系统更新错误0x80240034怎么办
- Windows10如何更改盘符名称_Win10重命
- 如何在 Go 中判断变量是否为函数类型
- c++怎么实现大文件的分块读写_c++ 文件指针s
- C#如何序列化对象为XML XmlSerializ
- 如何将竖排文本文件转换为横排字符串
- Win11如何设置省电模式 Win11开启电池节电
- Win11怎么设置开机自动连接宽带_Windows
- Windows11如何设置专注助手_Windows
- Windows10如何更改鼠标灵敏度_Win10鼠
- Linux怎么实现内网穿透_Linux安装Frp客
- Windows服务启动类型恢复方法_错误修改导致的
- Win11怎么设置ipv4地址_Windows 1
- php增删改查在php8里有什么变化_新特性对cu
- Win10怎样卸载DockerDesktop_Wi
- 如何在Golang中处理数据库事务错误_回滚和日志
- Win11 explorer.exe频繁崩溃_修复
- 如何使用Golang table-driven f
- c++怎么使用std::filesystem遍历文
- Windows10怎么查看系统激活状态_Windo
- Win11任务栏怎么固定应用 Win11将软件图标
- MAC怎么截图并快速编辑_MAC自带截图快捷键与标
- 如何使用Golang进行HTTP服务性能测试_测量
- Mac如何修复应用程序权限问题_Mac磁盘工具修复
- 如何使用Golang管理模块版本_Golanggo
- 本地php环境出现502错误_nginx或apac
- c++ unordered_map怎么用 c++哈
- Win11怎么关闭SmartScreen_禁用Wi
- Win10电脑怎么设置IP地址_Windows10
- Win11如何设置系统语言_Win11系统语言切换
- Windows执行文件被SmartScreen拦截
- 如何在 Go 中创建包含映射(map)的切片(sl
- PHP cURL GET请求:正确设置认证与自定义
- 怎么将XML数据可视化 D3.js加载XML
- 如何使用Golang defer优化性能_减少不必
- Win11时间不对怎么同步_Win11自动校准互联
- 如何优化Golang Web性能_Golang H
- Win11怎么设置默认邮件客户端 Win11修改M

95f7d');
Empty set (0.02 sec)
QQ客服