Oracle start with connect by prior 用法

來源:互聯網
上載者:User

標籤:style   io   ar   使用   sp   strong   資料   div   on   

Oracle start with connect by prior 用法   
文法: select * from 表名 where 條件1 start with 條件2 connect by prior 當前表欄位=級聯表欄位 start with與connect by prior陳述式完成遞迴記錄,形成一棵樹形結構,通常可以在具有階層的表中使用。 start with表示開始的記錄 connect by prior 指定與目前記錄關聯時的欄位關係代碼: 
--建立部門表,這是一個具有階層的表,子記錄通過parent_id與父記錄的id進行關聯 create table DEPT( ID NUMBER(9) PRIMARY KEY, --部門ID NAME VARCHAR2(100), 
--部門名稱 PARENT_ID NUMBER(9) 
--父級部門ID,通過此欄位與上層業務關聯 ); 向表中插入如下資料,為了使代碼簡單,一個部門僅具有一個下級部門●從根節點開始查詢遞迴的記錄 select * from dept start with id=1 connect by prior id = parent_id; 下面是查詢結果,start with id=1表示從id=1的記錄開始查詢,向葉子的方向遞迴,遞迴條件是id=parent_id,目前記錄的id等於子記錄的parent_id 
●從葉子節點開始查詢遞迴的記錄 select * from dept start with id=5 connect by prior parent_id = id; 
下面是查詢結果,遞迴條件按照目前記錄的parent_id等與父記錄的id 
●對查詢結果過濾 select * from dept where name like ‘%銷售%‘ start with id=1 connect by prior id = parent_id; 在下面的查詢結果中可以看到,首先使用start with... connect by prior查詢出樹形的結構,然後where條件才生效,對全部查詢結果進行過濾 
●prior的作用 prior關鍵字表示不進行遞迴查詢,僅查詢出滿足id=1的記錄,下面是將第一個查詢去掉prior關鍵字後結果 select * from dept start with id=1 connect by prior id = parent_id;

Oracle start with connect by prior 用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.