濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > MySQL之遞歸小問(wèn)題

MySQL之遞歸小問(wèn)題

熱門標(biāo)簽:沈陽(yáng)電銷外呼系統(tǒng)原理是什么 地圖標(biāo)注人員兼職 扎樣申請(qǐng)400電話 聊城智能外呼系統(tǒng)運(yùn)營(yíng)商 電子地圖標(biāo)注電話 ps制作地圖標(biāo)注gif 寧波企業(yè)外呼系統(tǒng)收費(fèi) 上海智能外呼系統(tǒng)代理商 成都優(yōu)派外呼系統(tǒng)

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

--遞歸小方法:臨時(shí)表和普通表的不同方法
--這題使用的是2次臨時(shí)表查詢父節(jié)點(diǎn)的遞歸 

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))
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 | 大學(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)

上面的方法因?yàn)橛捎贛ySQL中不允許在同一語(yǔ)句中對(duì)臨時(shí)表多次引用,所以用2次臨時(shí)表
下面給個(gè)一次性用普通表完成的 查詢子節(jié)點(diǎn)的遞歸查詢

核心代碼

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遞歸查詢樹(shù)狀表的子節(jié)點(diǎn)、父節(jié)點(diǎn)具體實(shí)現(xiàn)
  • SQL如何實(shí)現(xiàn)MYSQL的遞歸查詢
  • php+mysql不用遞歸實(shí)現(xiàn)的無(wú)限級(jí)分類實(shí)例(非遞歸)
  • 使用遞歸刪除樹(shù)形結(jié)構(gòu)的所有子節(jié)點(diǎn)(java和mysql實(shí)現(xiàn))
  • 使用函數(shù)遞歸實(shí)現(xiàn)基于php和MySQL的動(dòng)態(tài)樹(shù)型菜單
  • 利用java+mysql遞歸實(shí)現(xiàn)拼接樹(shù)形JSON列表的方法示例
  • PHP遞歸寫(xiě)入MySQL實(shí)現(xiàn)無(wú)限級(jí)分類數(shù)據(jù)操作示例
  • Mysql樹(shù)形遞歸查詢的實(shí)現(xiàn)方法

標(biāo)簽:朔州 AXB 三明 宿州 林芝 內(nèi)江 咸寧 汕頭

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL之遞歸小問(wèn)題》,本文關(guān)鍵詞  MySQL,之,遞歸,小,問(wèn)題,MySQL,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MySQL之遞歸小問(wèn)題》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于MySQL之遞歸小問(wèn)題的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    鹤岗市| 广宁县| 舟曲县| 景谷| 麟游县| 岳池县| 平南县| 渑池县| 云梦县| 容城县| 罗田县| 唐山市| 息烽县| 扬州市| 民丰县| 邵阳县| 麟游县| 许昌县| 云安县| 昌宁县| 沧州市| 枣阳市| 绥宁县| 射洪县| 临夏县| 藁城市| 鞍山市| 安陆市| 辉县市| 滨州市| 太仆寺旗| 诏安县| 墨江| 高陵县| 泸定县| 平乡县| 凤山县| 石嘴山市| 湖南省| 吉隆县| 河西区|