SQL的遞歸查詢多級菜單


with tmpTable
as
(
-- 1、根節點
select * from tableName where parentId = 'xxx'
union all
-- 2、遞歸條件
select a.* from tableName a inner join tmpTable b on a.parentId = b.id
)
select * from tmpTable;

 

參考:https://blog.csdn.net/shijie_nihao/article/details/100717147

 

上面是向下查詢的,下面的是向上查詢的,其實代碼是一樣的,只是把 a.id= b.parentId 換一下

with tmpTable
as
(
-- 1、根節點
select * from tableName where parentId = 'xxx'
union all
-- 2、遞歸條件
select a.* from tableName a inner join tmpTable b on a.id= b.parentId 
)
select * from tmpTable;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM