Oracle遞迴查詢

來源:互聯網
上載者:User

標籤:oracle遞迴查詢

                ----建立資料

1.1、建立表與插入資料

650) this.width=650;" src="http://common.cnblogs.com/images/copycode.gif" alt="複製代碼" />

CREATE TABLE DISTRICT(  ID         NUMBER(10)                  NOT NULL,  PARENT_ID  NUMBER(10),  NAME       VARCHAR2(255 BYTE)          NOT NULL);ALTER TABLE DISTRICT ADD (  CONSTRAINT DISTRICT_PK PRIMARY KEY (ID));ALTER TABLE DISTRICT ADD (  CONSTRAINT DISTRICT_R01  FOREIGN KEY (PARENT_ID)  REFERENCES DISTRICT (ID));   insert into DISTRICT (id, parent_id, name)values (1, null, ‘四川省‘);insert into DISTRICT (id, parent_id, name)values (2, 1, ‘巴中市‘);insert into DISTRICT (id, parent_id, name)values (3, 1, ‘達州市‘);insert into DISTRICT (id, parent_id, name)values (4, 2, ‘巴州區‘);insert into DISTRICT (id, parent_id, name)values (5, 2, ‘通江縣‘);insert into DISTRICT (id, parent_id, name)values (6, 2, ‘平昌縣‘);insert into DISTRICT (id, parent_id, name)values (7, 3, ‘通川區‘);insert into DISTRICT (id, parent_id, name)values (8, 3, ‘宣漢縣‘);insert into DISTRICT (id, parent_id, name)values (9, 8, ‘塔河鄉‘);insert into DISTRICT (id, parent_id, name)values (10, 8, ‘三河鄉‘);insert into DISTRICT (id, parent_id, name)values (11, 8, ‘胡家鎮‘);insert into DISTRICT (id, parent_id, name)values (12, 8, ‘南壩鎮‘);insert into DISTRICT (id, parent_id, name)values (13, 6, ‘大寨鄉‘);insert into DISTRICT (id, parent_id, name)values (14, 6, ‘響灘鎮‘);insert into DISTRICT (id, parent_id, name)values (15, 6, ‘龍崗鎮‘);insert into DISTRICT (id, parent_id, name)values (16, 6, ‘白衣鎮‘);commit;

650) this.width=650;" src="http://images2015.cnblogs.com/blog/710715/201511/710715-20151113140234853-565754984.jpg" />

二、start with connect by prior遞迴
  1. 查詢所有子節點

SELECT *
FROM district
START WITH NAME =‘巴中市‘
CONNECT BY PRIOR ID=parent_id


650) this.width=650;" src="http://images2015.cnblogs.com/blog/710715/201511/710715-20151113140429712-390342453.jpg" />

2.2、查詢所有父節點

SELECT *
FROM district
START WITH NAME =‘平昌縣‘
CONNECT BY PRIOR parent_id=ID


只需要交換 id 與parent_id的位置即可


650) this.width=650;" src="http://images2015.cnblogs.com/blog/710715/201511/710715-20151113140553978-1462460137.jpg" />



2.3、查詢指定節點的,根節點

650) this.width=650;" src="http://common.cnblogs.com/images/copycode.gif" alt="複製代碼" />

SELECT d.*,connect_by_root(d.id),connect_by_root(NAME)FROM district dWHERE NAME=‘平昌縣‘START WITH d.parent_id=1    --d.parent_id is null 結果為四川省CONNECT BY PRIOR d.ID=d.parent_id

650) this.width=650;" src="http://common.cnblogs.com/images/copycode.gif" alt="複製代碼" />

650) this.width=650;" src="http://images2015.cnblogs.com/blog/710715/201511/710715-20151113141339025-664756404.jpg" />

2.4、查詢巴中市下行政組織遞迴路徑

 

SELECT ID,parent_id,NAME,sys_connect_by_path(NAME,‘->‘) namepath,LEVELFROM district START WITH NAME=‘巴中市‘CONNECT BY PRIOR ID=parent_id

650) this.width=650;" src="http://images2015.cnblogs.com/blog/710715/201511/710715-20151113141811244-1376303505.jpg" />

三、with遞迴

3.1、with遞迴子類

 

650) this.width=650;" src="http://common.cnblogs.com/images/copycode.gif" alt="複製代碼" />

WITH t (ID ,parent_id,NAME) --要有列名AS(SELECT ID ,parent_id,NAME FROM district WHERE NAME=‘巴中市‘UNION ALLSELECT d.ID ,d.parent_id,d.NAME FROM t,district d --要指定表和列表,WHERE t.id=d.parent_id)SELECT * FROM t;

650) this.width=650;" src="http://common.cnblogs.com/images/copycode.gif" alt="複製代碼" />

 

650) this.width=650;" src="http://images2015.cnblogs.com/blog/710715/201511/710715-20151113142500759-1277968503.jpg" />

3.2、遞迴父類

650) this.width=650;" src="http://common.cnblogs.com/images/copycode.gif" alt="複製代碼" />

WITH t (ID ,parent_id,NAME) --要有表AS(SELECT ID ,parent_id,NAME FROM district WHERE NAME=‘通江縣‘UNION ALLSELECT d.ID ,d.parent_id,d.NAME FROM t,district d --要指定表和列表,WHERE t.parent_id=d.id)SELECT * FROM t;

650) this.width=650;" src="http://common.cnblogs.com/images/copycode.gif" alt="複製代碼" />

650) this.width=650;" src="http://images2015.cnblogs.com/blog/710715/201511/710715-20151113143231431-1925487760.jpg" />

 



本文出自 “事在人為,知在天意” 部落格,請務必保留此出處http://yangsj.blog.51cto.com/8702844/1714358

Oracle遞迴查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.