IT忍者神龜之oracle connect by用法回顧,忍者神龜oracle

來源:互聯網
上載者:User

IT忍者神龜之oracle connect by用法回顧,忍者神龜oracle

先用scott使用者下的emp表做實驗.
emp表有個欄位,一個是empno(員工編號),另一個是mgr(上級經理編號)
下面是表中所有資料

1 select *from emp start with empno=7698connect by  mgr=prior empno;

執行結果如下:

得到的結果是empno=7698的資料,以及會得到mgr=7698的資料。
它是向下遞迴的, 即我們從empno=7698開始遍曆,去找出mgr=7698的所有資料S(用S代表查出的所有資料.), 然後在從S中的empno的值去匹配尋找是否還有滿足,mgr in (s.empno)的資料。一直遍曆進去到沒有資料為止。 

 下面的這個可以詳細的表述效果。

12 --向下遞迴遍曆select *from empconnect by mgr=prior empno start with empno=7839;

執行結果如下:

12 --向上遞迴遍曆select * from emp connect by prior mgr=empno start with empno=7844;

執行結果如下:

這樣直到沒有匹配的資料為止。
以上只是簡單的舉了個例子。

connect by是結構化查詢中用到的,其基本文法是:

1234 select ...from tablenamestartby cond1connect by cond2where cond3

簡單說來是將一個樹狀結構儲存在一張表裡,比如一個表中存在兩個欄位(如emp表中的empno和mgr欄位):empno, mgr那麼通過表示每一條記錄的mgr是誰,就可以形成一個樹狀結構。

用上述文法的查詢可以取得這棵樹的所有記錄。
其中:
cond1是根結點的限定語句,當然可以放寬限定條件,以取得多個根結點,實際就是多棵樹。
cond2是串連條件,其中用prior表示上一條記錄,比如connect by prior id=praentid就是說上一條記錄的id是本條記錄的praentid,即本記錄的父親是上一條記錄。
cond3是過濾條件,用於對返回的所有記錄進行過濾。
prior和start with關鍵字是可選項
prior運算子必須放置在串連關係的兩列中某一個的前面。對於節點間的父子關係,prior運算子在一側表示父節點,在另一側表示子節點,從而確定尋找樹結構是的順序是自頂向下還是自底向上。在串連關係中,除了可以使用列名外,還允許使用列運算式。
start with子句為可選項,用來標識哪個節點作為尋找樹型結構的根節點。若該子句被省略,則表示所有滿足查詢條件的行作為根節點。


oracle connect by 與 start with 的用法

遞迴查詢,當資料結構呈現為樹形、鏈表形時非常有效。
例如,菜單、組織機構等。
 
connect by prior用qbc怎實現

Oracle Connect By用法

oracle中的select語句可以用START WITH...CONNECT BY PRIOR子句實現遞迴查詢,connect by 是結構化查詢中用到的,其基本文法是:

select ... from <TableName>
where <Conditional-1>
start with <Conditional-2>
connect by <Conditional-3>
;

<Conditional-1>:過濾條件,用於對返回的所有記錄進行過濾。
<Conditional-2>:查詢結果重起始根結點的限定條件。
<Conditional-3>:串連條件

例如:

資料庫表結構如下:

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');

1.擷取完整樹:

select * from t2;
select * from t2 start with root_id = 0 connect by prior id = root_id;

2.擷取特定子樹:
select * from t2 start with id = 1 connect by prior id = root_id;

select * from t2 start with id = 4 connect by prior id = root_id;

3.如果connect by prior中的prior被省略,則查詢將不進行深層遞迴。
如:
select * from t2 start with root_id = 0 connect by id = root_id;

select * from t2 start with id = 1 connect by id = root_id;

1) prior:被放置於等號前後的位置,決定著查詢時的檢索順序。
PRIOR被置於CONNECT BY子句中等號的前面時,則強制從根節點到分葉節點的順序檢索,即由父節點向......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.