如何使用資料庫實現遞迴查詢

2021-09-12 08:44:28 字數 4906 閱讀 3394

sql server 2005開始,我們可以直接通過cte來支援遞迴查詢,cte即公用表表示式

公用表表示式(cte),是乙個在查詢中定義的臨時命名結果集將在from子句中使用它。每個cte僅被定義一次(但在其作用域內可以被引用任意次),並且在該查詢生存期間將一直生存。可以使用cte來執行遞迴操作。建立的語法是:

with ()

as(

) select * from 生成資料

--選單目錄結構表

create table tb_menu(

id int not null, --主鍵id

title varchar(50), --標題

parent int --parent id

); --父選單

insert into tb_menu(id, title, parent) values(1, '父選單1',null);

insert into tb_menu(id, title, parent) values(2, '父選單2',null);

insert into tb_menu(id, title, parent) values(3, '父選單3',null);

insert into tb_menu(id, title, parent) values(4, '父選單4',null);

insert into tb_menu(id, title, parent) values(5, '父選單5',null);

--一級選單

insert into tb_menu(id, title, parent) values(6, '一級選單6',1);

insert into tb_menu(id, title, parent) values(7, '一級選單7',1);

insert into tb_menu(id, title, parent) values(8, '一級選單8',1);

insert into tb_menu(id, title, parent) values(9, '一級選單9',2);

insert into tb_menu(id, title, parent) values(10, '一級選單10',2);

insert into tb_menu(id, title, parent) values(11, '一級選單11',2);

insert into tb_menu(id, title, parent) values(12, '一級選單12',3);

insert into tb_menu(id, title, parent) values(13, '一級選單13',3);

insert into tb_menu(id, title, parent) values(14, '一級選單14',3);

insert into tb_menu(id, title, parent) values(15, '一級選單15',4);

insert into tb_menu(id, title, parent) values(16, '一級選單16',4);

insert into tb_menu(id, title, parent) values(17, '一級選單17',4);

insert into tb_menu(id, title, parent) values(18, '一級選單18',5);

insert into tb_menu(id, title, parent) values(19, '一級選單19',5);

insert into tb_menu(id, title, parent) values(20, '一級選單20',5);

--二級選單

insert into tb_menu(id, title, parent) values(21, '二級選單21',6);

insert into tb_menu(id, title, parent) values(22, '二級選單22',6);

insert into tb_menu(id, title, parent) values(23, '二級選單23',7);

insert into tb_menu(id, title, parent) values(24, '二級選單24',7);

insert into tb_menu(id, title, parent) values(25, '二級選單25',8);

insert into tb_menu(id, title, parent) values(26, '二級選單26',9);

insert into tb_menu(id, title, parent) values(27, '二級選單27',10);

insert into tb_menu(id, title, parent) values(28, '二級選單28',11);

insert into tb_menu(id, title, parent) values(29, '二級選單29',12);

insert into tb_menu(id, title, parent) values(30, '二級選單30',13);

insert into tb_menu(id, title, parent) values(31, '二級選單31',14);

insert into tb_menu(id, title, parent) values(32, '二級選單32',15);

insert into tb_menu(id, title, parent) values(33, '二級選單33',16);

insert into tb_menu(id, title, parent) values(34, '二級選單34',17);

insert into tb_menu(id, title, parent) values(35, '二級選單35',18);

insert into tb_menu(id, title, parent) values(36, '二級選單36',19);

insert into tb_menu(id, title, parent) values(37, '二級選單37',20);

--**選單

insert into tb_menu(id, title, parent) values(38, '**選單38',21);

insert into tb_menu(id, title, parent) values(39, '**選單39',22);

insert into tb_menu(id, title, parent) values(40, '**選單40',23);

insert into tb_menu(id, title, parent) values(41, '**選單41',24);

insert into tb_menu(id, title, parent) values(42, '**選單42',25);

insert into tb_menu(id, title, parent) values(43, '**選單43',26);

insert into tb_menu(id, title, parent) values(44, '**選單44',27);

insert into tb_menu(id, title, parent) values(45, '**選單45',28);

insert into tb_menu(id, title, parent) values(46, '**選單46',28);

insert into tb_menu(id, title, parent) values(47, '**選單47',29);

insert into tb_menu(id, title, parent) values(48, '**選單48',30);

insert into tb_menu(id, title, parent) values(49, '**選單49',31);

insert into tb_menu(id, title, parent) values(50, '**選單50',31);

commit;

2.查詢所有上級節點

--查詢樹狀結構某節點(44)的上級所有根節點

with cte_parent(id,title,parent)

as (

--起始條件

select id,title,parent

from tb_menu

where id = 44   --列出子節點查詢條件

union all

--遞迴條件

select a.id,a.title,a.parent

from tb_menu a

inner join 

cte_parent b          --執行遞迴,這裡就要理解下了 

on a.id=b.parent  

)                       

select * from cte_parent;

3.查詢下級節點帶level

--查詢樹狀結構某節點下的所有子節點(

with cte_child(id,title,parent,level)

as (

--起始條件

select id,title,parent,0 as level

from tb_menu

where id = 6--列出父節點查詢條件

union all

--遞迴條件

select a.id,a.title,a.parent,b.level+1

from tb_menu a

inner join 

cte_child b

on ( a.parent=b.id)  

) select  * from cte_child;

資料庫遞迴查詢

今天工作看同事 用到了start with.connect by prior,不知道什麼意思,查詢了一些資料,以下是比較好理解的。oracle中的select語句可以用start with.connect by prior子句實現遞迴查詢,connect by 是結構化查詢中用到的,其基本語法是 s...

C 實現資料庫的遞迴查詢

表結構如下 標識號 名稱 上級標識號 mid mname mparentid 1 選單一 0 2 選單二 1 3 選單三 1 4 選單四 2 5 選單五 3 要用遞迴程式實現某一標識號 包括本身 下的所有選單,要實現這個程式,我們這裡取出所有符合的選單標識號,用 連線成字串,實現的方法如下 取得選單...

資料庫遞迴查詢(CET)

if object id ta isnotnull droptable ta go createtableta id int,name nvarchar 4 parentid nvarchar 2 go insertintota select1,河北省 0 unionall select2,邢台市 ...