国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MySQL > 正文

MySQL之遞歸小問題

2024-07-24 13:13:25
字體:
供稿:網(wǎng)友

mysql本身不支持遞歸語法,但可通過自連接變相實現(xiàn)一些簡單的遞歸

--遞歸小方法:臨時表和普通表的不同方法--這題使用的是2次臨時表查詢父節(jié)點的遞歸 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', '大學(xué)教師', '37e2ea0a-1c31-3412-455a-5e60b8395f7d' union all select '1ce203ac-ee34-b902-6c10-c806f0f52876','小學(xué)教師', '37e2ea0a-1c31-3412-455a-5e60b8395f7d' union all select '37e2ea0a-1c31-3412-455a-5e60b8395f7d', '教師' ,      null                union all select 'c877b7ea-4ed3-f472-9527-53e1618cb1dc', '高數(shù)老師', '13ed38f1-3c24-dd81-492f-673686dff0f3' union all select 'ce50a471-2955-00fa-2fb7-198f6b45b1bd', '中學(xué)教師', '37e2ea0a-1c31-3412-455a-5e60b8395f7d'; delimiter $$ create procedure usp_ser(in idd varchar(100))begindeclare 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 )dotruncate 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 | 大學(xué)教師 | 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次臨時表
下面給個一次性用普通表完成的 查詢子節(jié)點的遞歸查詢

核心代碼

drop table if exists test;create table test(id INT,parentid INT);insert test select1, 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))begindeclare 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()>0doset 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 |+------+----------+------+-------+*/


注:相關(guān)教程知識閱讀請移步到MYSQL教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 土默特左旗| 清流县| 元谋县| 龙里县| 阳原县| 句容市| 永康市| 澄城县| 调兵山市| 辉县市| 崇信县| 新泰市| 新野县| 武城县| 尉犁县| 阳西县| 海安县| 南开区| 天峨县| 义马市| 扬州市| 揭阳市| 连山| 桐乡市| 厦门市| 汨罗市| 永仁县| 武鸣县| 鄢陵县| 赣榆县| 兰坪| 正定县| 德令哈市| 通许县| 全椒县| 台南市| 平和县| 桑植县| 芜湖市| 抚州市| 嫩江县|