神剑山庄资源网 Design By www.hcban.com
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 | +------+----------+------+-------+*/
标签:
MySQL,递归
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无MySQL之递归小问题的评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年10月19日
2024年10月19日
- 群星《歌手2024 第4期》[FLAC/分轨][563.76MB]
- RiffKitten-ChaosParade(2024)[24-44,1]FLAC
- RuneKlakegg-Nattevandrer(2024)[24-96]\FLAC
- 赖冰霞.2006-金嗓典藏辑2IN1(南方金点系列)2CD【南方】【WAV+CUE】
- 群星.1988-国语金曲尽精英VOL.2【瑞成】【WAV+CUE】
- 张宇.1999-雨一直下【EMI百代】【WAV+CUE】
- 袁娅维.2021-月亮失眠了(DELUXE)【华纳】【FLAC分轨】
- AlanBroadbent-JazzFunk(2024)[24-44,1]
- 梅艳芳《绝代芳华》开盘母带[低速原抓WAV+CUE]
- ABC唱片-《外国精逊母带直刻神奇黑胶[APE+CUE].
- 群星《天赐的声音第五季 第6期》[320K/MP3][101.43MB]
- 群星《天赐的声音第五季 第6期》[FLAC/分轨][526.16MB]
- 群星《说唱梦工厂 第2期》[320K/MP3][96.37MB]
- 杨宗宪.1994-留你留袂着【有容唱片】【WAV+CUE】
- 李翊君.2005-勇敢的爱【创意唱片】【WAV+CUE】