MySQL之遞歸小問題
更新時(shí)間:2017年05月20日 14:04:32 投稿:mdxy-dxy
這篇文章主要介紹了MySQL之遞歸小問題,需要的朋友可以參考下
mysql本身不支持遞歸語法,但可通過自連接變相實(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中不允許在同一語句中對(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遞歸查詢樹狀表的子節(jié)點(diǎn)、父節(jié)點(diǎn)具體實(shí)現(xiàn)
- SQL如何實(shí)現(xiàn)MYSQL的遞歸查詢
- php+mysql不用遞歸實(shí)現(xiàn)的無限級(jí)分類實(shí)例(非遞歸)
- 使用遞歸刪除樹形結(jié)構(gòu)的所有子節(jié)點(diǎn)(java和mysql實(shí)現(xiàn))
- 使用函數(shù)遞歸實(shí)現(xiàn)基于php和MySQL的動(dòng)態(tài)樹型菜單
- 利用java+mysql遞歸實(shí)現(xiàn)拼接樹形JSON列表的方法示例
- PHP遞歸寫入MySQL實(shí)現(xiàn)無限級(jí)分類數(shù)據(jù)操作示例
- Mysql樹形遞歸查詢的實(shí)現(xiàn)方法
相關(guān)文章
MySQL多層級(jí)結(jié)構(gòu)-樹搜索介紹
這篇文章主要介紹了MySQL多層級(jí)結(jié)構(gòu)-樹搜索,需要的朋友可以參考下2016-07-07mysql之查找所有數(shù)據(jù)庫中沒有主鍵的表問題
這篇文章主要介紹了mysql之查找所有數(shù)據(jù)庫中沒有主鍵的表問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03IP處理函數(shù)inet_aton()和inet_ntoa()使用說明
IP處理函數(shù)inet_aton()和inet_ntoa()使用說明,需要的朋友可以參考下2012-03-03MySQL用戶和權(quán)限及破解root口令的方法示例
這篇文章主要介紹了詳解MySQL用戶和權(quán)限及破解root口令,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05深入SQL Server中char、varchar、text和nchar、nvarchar、ntext的區(qū)別詳
本篇文章是對(duì)char、varchar、text和nchar、nvarchar、ntext的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06