oracle遞迴查詢子節點

來源:互聯網
上載者:User

標籤:blog   art   rom   title   ber   預設   first   www   rac   

通過子節點向根節點追朔.

select * from persons.dept start with deptid=76 connect by prior paredeptid=deptid

通過根節點遍曆子節點(不包含根節點).

select * from persons.dept start with paredeptid=0 connect by prior deptid=paredeptid

通過根節點遍曆子節點(包含根節點).

select * from persons.dept start with deptid=0 connect by prior deptid=paredeptid

可通過level 關鍵字查詢所在層次.

select a.*,level from persons.dept a start with paredeptid=0 connect by prior deptid=paredeptid

備忘:
start with ...connect by 的用法, start with 後面所跟的就是就是遞迴的種子。
遞迴的種子也就是遞迴開始的地方 connect by 後面的"prior" 如果預設:則只能查詢到合格起始行,並不進行遞迴查詢;
connect by prior 後面所放的欄位是有關係的,它指明了查詢的方向。

練習: 通過子節點獲得頂節點
select FIRST_VALUE(deptid) OVER (ORDER BY LEVEL DESC ROWS UNBOUNDED PRECEDING) AS firstdeptid from persons.dept start with deptid=76 connect by prior paredeptid=deptid
表結構
DEPTID PAREDEPTID NAME
NUMBER NUMBER CHAR (40 Byte)
部門id 父部門id(所屬部門id) 部門名稱

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.