對于oracle進(jìn)行簡(jiǎn)單樹(shù)查詢(xún)(遞歸查詢(xún))
DEPTID PAREDEPTID NAME
NUMBER NUMBER CHAR (40 Byte)
部門(mén)id 父部門(mén)id(所屬部門(mén)id) 部門(mén)名稱(chēng)
通過(guò)子節點(diǎn)向根節點(diǎn)追朔.
Sql代碼
select * from persons.dept start with deptid=76 connect by prior paredeptid=deptid
select * from persons.dept start with deptid=76 connect by prior paredeptid=deptid
通過(guò)根節點(diǎn)遍歷子節點(diǎn).
Sql代碼
select * from persons.dept start with paredeptid=0 connect by prior deptid=paredeptid
select * from persons.dept start with paredeptid=0 connect by prior deptid=paredeptid
可通過(guò)level 關(guān)鍵字查詢(xún)所在層次.
Sql代碼
select a.*,level from persons.dept a start with paredeptid=0 connect by prior deptid=paredeptid
select a.*,level from persons.dept a start with paredeptid=0 connect by prior deptid=paredeptid
再次復習一下:start with ...connect by 的用法, start with 后面所跟的就是就是遞歸的種子。
遞歸的種子也就是遞歸開(kāi)始的地方 connect by 后面的"prior" 如果缺?。簞t只能查詢(xún)到符合條件的起始行,并不進(jìn)行遞歸查詢(xún);
connect by prior 后面所放的字段是有關(guān)系的,它指明了查詢(xún)的方向。
練習: 通過(guò)子節點(diǎn)獲得頂節點(diǎn)
Sql代碼
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
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。