oracle中的select語句可以用start with...connect by prior子句實現遞迴查詢,connect by 是結構化查詢中用到的,其基本語法是:
select ... from
where
start with
connect by
;:過濾條件,用於對返回的所有記錄進行過濾。
:查詢結果重起始根結點的限定條件。
:連線條件
資料組織結構如下圖:
|---a------a1
| |------a2
||---b------b1
|------b2
資料庫表結構如下:
create table t2(
root_id number,
id number,
name varchar(5),
description varchar(10)
);insert into t2(root_id,id,name,description) values(0,1,'a','aaa');
insert into t2(root_id,id,name,description) values(1,2,'a1','aaa1');
insert into t2(root_id,id,name,description) values(1,3,'a2','aaa2');
insert into t2(root_id,id,name,description) values(0,4,'b','bbb');
insert into t2(root_id,id,name,description) values(4,5,'b1','bbb1');
insert into t2(root_id,id,name,description) values(4,6,'b2','bbb2');
sql> select * from t2;
root_id id name descriptio
---------- ---------- ----- ----------
0 1 a aaa
1 2 a1 aaa1
1 3 a2 aaa2
0 4 b bbb
4 5 b1 bbb1
4 6 b2 bbb2
獲取完整樹:
select * from t2 start with root_id = 0 connect by prior id = root_id;
|---a------a1
| |------a2
||---b------b1
|------b2
root_id id name descriptio
---------- ---------- ----- ----------
0 1 a aaa
1 2 a1 aaa1
1 3 a2 aaa2
0 4 b bbb
4 5 b1 bbb1
4 6 b2 bbb2
獲取特定子樹:
select * from t2 start with id = 1 connect by prior id = root_id;
|---a------a1
| |------a2
root_id id name descriptio
---------- ---------- ----- ----------
0 1 a aaa
1 2 a1 aaa1
1 3 a2 aaa2
select * from t2 start with id = 4 connect by prior id = root_id;
|---b------b1
|------b2
root_id id name descriptio
---------- ---------- ----- ----------
0 4 b bbb
4 5 b1 bbb1
4 6 b2 bbb2
如果connect by prior中的prior被省略,則查詢將不進行深層遞迴。
如:select * from t2 start with root_id = 0 connect by id = root_id;
|---a
|
||---b
root_id id name descriptio
---------- ---------- ----- ----------
0 1 a aaa
0 4 b bbb
如:
select * from t2 start with id = 1 connect by id = root_id;
|---a
root_id id name descriptio
---------- ---------- ----- ----------
0 1 a aaa
oracle中累計求和 oracle累計求和
poj2001 shortest prefixes trie樹應用 沉迷wow又頹了兩天orz,暴雪爸爸要在國服出月卡了.這是要我好好學習嗎?趕緊來刷題了.oj 題目大意是求所有字串裡每乙個字元 硬體相關 jtag介面 jtag joint test action group,聯合測試行動小組 是一...
oracle中累計求和 oracle累計求和
oracle累計求和 將當前行某列的值與前面所有行的此列值相加,即累計求和 方法一 with t as select 1 val from dual union all select 3 from dual union all select 5 from dual union all select ...
Oracle中臨時表
最近考慮到我們的資料庫端寫儲存過程關於臨時表使用的情況,由於我們 現在還不清楚資料庫端到底是怎麼處理的,是否和sql server的處理方式相 同,是否會存在隱患等等一些問題,為了避免將來不必要的麻煩我做了深 入的研究和檢視了一些權威的資料,現在和大家共享,希望大家在處理 oracle臨時表是注意一...