Oracle遞迴查詢,Oracle遞迴
建立表和主外鍵
CREATE TABLE SC_DISTRICT( ID NUMBER(10) NOT NULL, PARENT_ID NUMBER(10), NAME VARCHAR2(255 BYTE) NOT NULL);ALTER TABLE SC_DISTRICT ADD ( CONSTRAINT SC_DISTRICT_PK PRIMARY KEY (ID));ALTER TABLE SC_DISTRICT ADD ( CONSTRAINT SC_DISTRICT_R01 FOREIGN KEY (PARENT_ID) REFERENCES SC_DISTRICT (ID));
插入資料
insert into A_DISTRICT (id, parent_id, name) values (1, null, '河南省');insert into A_DISTRICT (id, parent_id, name) values (2, null, '山東省');insert into A_DISTRICT (id, parent_id, name) values (3, 1, '鄭州市');insert into A_DISTRICT (id, parent_id, name) values (4, 1, '開封市');insert into A_DISTRICT (id, parent_id, name) values (5, 1, '商丘市');insert into A_DISTRICT (id, parent_id, name) values (6, 3, '中原區');insert into A_DISTRICT (id, parent_id, name) values (7, 3, '金水區');insert into A_DISTRICT (id, parent_id, name) values (8, 3, '二七區');insert into A_DISTRICT (id, parent_id, name) values (9, 3, '城管回族區');insert into A_DISTRICT (id, parent_id, name) values (10, 3, '上街區');insert into A_DISTRICT (id, parent_id, name) values (11, 3, '惠濟區');insert into A_DISTRICT (id, parent_id, name) values (12, 4, '開封縣');insert into A_DISTRICT (id, parent_id, name) values (13, 4, '杞縣');insert into A_DISTRICT (id, parent_id, name) values (14, 4, '蘭考縣');insert into A_DISTRICT (id, parent_id, name) values (15, 4, '通許縣');insert into A_DISTRICT (id, parent_id, name) values (16, 13, '城關鎮');insert into A_DISTRICT (id, parent_id, name) values (17, 13, '五裡河鎮');insert into A_DISTRICT (id, parent_id, name) values (18, 13, '邢口鎮');insert into A_DISTRICT (id, parent_id, name) values (19, 13, '柿園鄉');insert into A_DISTRICT (id, parent_id, name) values (20, 13, '城郊鄉');
產生資料如下
遞迴文法和主要使用函數
select * from 表 start with 條件入口 connect by prior id = parent_id(向下)
select * from 表 start with 條件入口 connect by prior parent_id = id(向上)
如果不寫prior,表明前序走訪 未指明遍曆方向,所以不進行遞迴,習慣上會在語句後面加上order by排序和group by分組
connect_by_root(列名) 查詢根節點
connect_by_isleaf 查詢是否葉子節點,0是,1不是
sys_connect_by_path 查詢遞迴路徑
實戰查詢根節點
select id, parent_id, name from a_district start with parent_id is null connect by id = parent_id order by id
遞迴查詢 杞縣 的所有上級節點(包含當前節點)
select id, parent_id, name from a_district start with name='杞縣' connect by prior parent_id = id order by id
遞迴查詢 杞縣 的所有上級節點(包含當前節點和根節點)
select id, parent_id, name,connect_by_root(id) city_id, connect_by_root(name) city_name from a_district start with name='杞縣' connect by prior parent_id = id order by id
遞迴查詢 杞縣 的所有子城鎮鄉(包含當前節點)
select id, parent_id, name from a_district start with name='杞縣' connect by prior id = parent_id order by id
遞迴查詢 開封市 的所有城鎮鄉(包含當前節點和根節點)
select id, parent_id, name, connect_by_root(id) city_id, connect_by_root(name) city_name from a_district start with name='開封市' connect by prior id = parent_id order by id
上行遞迴查詢 城郊鄉 深度、是否包含葉子節點
select id, name, parent_id, level, connect_by_isleaf isleaf from a_district start with name = '城郊鄉' connect by prior parent_id = id order by id
下行遞迴查詢 河南省 深度、是否包含葉子節點
select id, name, parent_id, level, connect_by_isleaf isleaf from a_district start with name = '河南省' connect by prior id = parent_id order by id
上行遞迴查詢 杞縣 路徑
select id, name, parent_id, substr(sys_connect_by_path(name,'->'),3) name_path from a_district start with name='杞縣' connect by prior parent_id = id order by id
下行遞迴查詢 河南省 路徑
select id, name, parent_id, substr(sys_connect_by_path(name,'->'),3) name_path from a_district start with name='河南省' connect by prior id = parent_id order by id
綜合使用
select id, name, parent_id, level, connect_by_isleaf isleaf, substr(sys_connect_by_path(name,'->'),3) name_path from a_district start with name='河南省' connect by prior id = parent_id order by id