create table test (
id int ,
pid int ,
name varchar(10))
insert into test values ( 1, 0 ,'n1' );
insert into test values ( 2, 1 ,'n12' );
insert into test values ( 3, 1 ,'n13' );
insert into test values ( 4, 2 ,'n21' );
insert into test values ( 5, 2 ,'n22' );
insert into test values ( 6, 3 ,'n31' );
insert into test values ( 7, 3 ,'n32' );
insert into test values ( 8, 4 ,'n211' );
insert into test values ( 9, 4 ,'n212' );
//從葉結點開始找根節點
// prior 關鍵字 與誰放在一起,就是找誰
select * from test
start with id=7
connect by id = prior pid;
// 從根結點開始找子節點
select * from test
start with id=2
connect by prior id = pid;
//選擇某層的下級節點
// level 是oracle 保留的級數字段
select id,name ,level from test
where level <=2
start with id =2
connect by prior id = pid;
-- 查出 7839 總裁下面各級 員工的工資和
select level , sum(sal)
from emp
group by level
start with empno = 7839
connect by prior empno = mgr
Oracle樹形查詢
基本語法 select.from tabename start with cond1 connect by prior cond2 where cond2 注 cond1是根節點的限定語句 cond2是連線條件,其中prior表示上一條記錄,指該記錄的父親是上一條記錄 cond3是過濾條件 構造環境...
oracle 樹形查詢
1.1 簡單的樹形查詢 select empno as 員工編碼,ename as 姓名,mgr as 主管,prior ename as 主管姓名 1.2 根節點,分支節點,葉節點 level 返回所在的等級 connect by isleaf 如果當前節點下沒有其他節點返回1,其他返回0。sel...
ORACLE樹形結構查詢
在oracle資料庫查詢中,我們經常會遇到對樹型結構表的查詢,這是個麻煩的問題。下面給大家介紹一種sql語句,實現遞迴查詢。語法如下 select 欄位1,欄位2,欄位3,from 表名 start with 條件1 connect by prior 條件2 where 條件3 下面舉乙個例子,有這...