Oracle 樹狀結構查詢

2021-07-25 19:55:47 字數 2053 閱讀 3975



【oracle】樹狀結構查詢

[sql] 

--oracle中的樹狀結構查詢實際上就是乙個中序遍歷  

--顯示以king為根節點的樹,start with定義起始節點  

select *  

from   emp a  

start  with a.empno = 7839  

connect by prior a.empno = a.mgr;  

--置於等號前面,由父節點向子節點方向檢索;置於等號後面,則從子節點向父節點方向檢索    

select *  

from   emp a  

start  with a.empno = 7839  

connect by prior a.empno = a.mgr;  

--使用偽列level來顯示層級關係  

select *  

from   emp a  

start  with a.empno = 7839  

connect by prior a.empno = a.mgr;  

select level, a.*  

from   emp a  

start  with a.empno = 7839  

connect by prior a.empno = a.mgr;  

--使用函式 sys_connect_by_path 顯示全路徑  

select level, sys_connect_by_path(a.ename, '/'), a.*  

from   emp a  

start  with a.empno = 7839  

connect by prior a.empno = a.mgr;  

--where子句只限定單個節點,不影響子其子節點或者父節點  

select level, sys_connect_by_path(a.ename, '/'), a.*  

from   emp a  

where a.empno <> 7369  

start  with a.empno = 7839  

connect by prior a.empno = a.mgr;  

--connect by 子句限定整個以當前節點為起始節點的整個分支  

--connect_by_root 可以在列之前顯示最高節點的內容  

select level, connect_by_root(a.job),sys_connect_by_path(a.ename, '/'), a.*  

from   emp a  

where a.empno <> 7369  

start  with a.empno = 7839  

connect by prior a.empno = a.mgr;  

--connect_by_isleaf 顯示當前行是否葉子節點,1:是;0:否  

select level, connect_by_isleaf,sys_connect_by_path(a.ename, '/'), a.*  

from   emp a  

where a.empno <> 7369  

start  with a.empno = 7839  

connect by prior a.empno = a.mgr;  

--connect_by_iscycle 10g中開始有的新特性,用來判斷當前節點是否產生了迴圈,0:否;1:是  

select level, connect_by_iscycle, connect_by_isleaf,sys_connect_by_path(a.ename, '/'), a.*  

from   emp a  

where a.empno <> 7369  

start  with a.empno = 7839  

connect by nocycle prior a.empno = a.mgr;  

Oracle 樹狀查詢

筱公尺加步槍 posted 2010年8月18日 23 01 in 資料庫 with tags oracle 樹狀,950 閱讀 在實際應用中,經常利用資料庫儲存樹狀結構的資料,通常用一張表中的兩個字段表示,乙個是自身的id,乙個是儲存父類的id。在這樣具有這種關係中的資料,要求查出的資料具有我們想...

oracle 樹狀查詢

connect by 是結構化查詢中用到的,其基本語法是 select from tablename start with 條件1 connect by 條件2 where 條件3 例 select from table start with org id hbhqfwgwpy connect by...

oracle 樹狀查詢

connect by 是結構化查詢中用到的,其基本語法是 select from tablename start with 條件1 connect by 條件2 where 條件3 例 select from table start with org id hbhqfwgwpy connect by...