1. 程式人生 > >oracle 樹查詢

oracle 樹查詢

oracle 通過 START WITH...CONNECT BY PRIOR 語法實現樹形查詢,查詢子節點與父節點,語法如下圖: start with:查詢所用根節點,可以有多個 connect by:確定子節點與父節點的關係,如  PRIOR c1 = c2 c1為父節點,通過c2來查詢c1 c1 = PRIOR c2 c2為父節點,通過c1來查詢c2 cycle: 預設為nocycle當樹形查詢為死迴圈時,會提示 "ORA-01436: CONNECT BY loop in user data"程式不執行 cycle,當樹形查詢為死迴圈時,繼續執行 其他 1.CONNECT_BY_ISLEAF偽列,當節點為葉節點時為1,否則為0
2.CONNECT_BY_ROOT,檢視欄位的根節點,語法 CONNECT_BY_ROOT COLUMN_NAME 3.SYS_CONNECT_BY_PATH,檢視當前節點路徑,語法 SYS_CONNECT_BY_PATH (column_name,'/') example id number (10) not null , --主鍵id name varchar2 (50), --標題 parent number (10) --parent id ) --父選單 insert into map( id, name , parent) values(1 , '亞洲', 0); insert
into map( id, name , parent) values(2 , '北美洲', 0); insert into map( id, name , parent) values(3 , '歐洲', 0); --一級選單 insert into map( id, name , parent) values(4 , '中國', 1); insert into map( id, name , parent) values(5 , '韓國', 1); insert into map( id, name , parent) values(6 , '日本', 1); insert into
map( id, name , parent) values(7 , '加拿大', 2); insert into map( id, name , parent) values(8 , '美國', 2); insert into map( id, name , parent) values(9 , '墨西哥', 2); insert into map( id, name , parent) values(10 , '英國', 3); insert into map( id, name , parent) values(11 , '法國', 3); insert into map( id, name , parent) values(12 , '義大利', 3); --二級選單 insert into map( id, name , parent) values(13 , '廣州', 4); insert into map( id, name , parent) values(14 , '上海', 4); insert into map( id, name , parent) values(15 , '北京', 4); insert into map( id, name , parent) values(16 , '首爾', 5); insert into map( id, name , parent) values(17 , ' 蔚山', 5); insert into map( id, name , parent) values(18 , '釜山', 5); insert into map( id, name , parent) values(19 , '東京', 6); insert into map( id, name , parent) values(20 , '大阪', 6); insert into map( id, name , parent) values(21 , '名古屋', 6); insert into map( id, name , parent) values(22 , '渥太華', 7); insert into map( id, name , parent) values(23 , '多倫多', 7); insert into map( id, name , parent) values(24 , '溫哥華', 7); insert into map( id, name , parent) values(25 , '華盛頓', 8); insert into map( id, name , parent) values(26 , '紐約', 8); insert into map( id, name , parent) values(27 , '舊金山', 8); insert into map