新聞中心
oracle樹查詢又稱為遞歸查詢,是最常用的查詢方法之一,下文對oracle樹查詢作了詳細(xì)的闡述,希望可以讓您對oracle樹查詢有更深的認(rèn)識。

oracle中的select語句可以用START WITH...CONNECT BY PRIOR子句實(shí)現(xiàn)遞歸查詢,connect by 是結(jié)構(gòu)化查詢中用到的,其基本語法是:
select * from tablename start with cond1
connect by cond2
where cond3;
簡單說來是將一個(gè)樹狀結(jié)構(gòu)存儲在一張表里,比如一個(gè)表中存在兩個(gè)字段:
id,parentid那么通過表示每一條記錄的parent是誰,就可以形成一個(gè)樹狀結(jié)構(gòu)。
用上述語法的查詢可以取得這棵樹的所有記錄。
其中COND1是根結(jié)點(diǎn)的限定語句,當(dāng)然可以放寬限定條件,以取得多個(gè)根結(jié)點(diǎn),實(shí)際就是多棵樹。
COND2是連接條件,其中用PRIOR表示上一條記錄,比如 CONNECT BY PRIOR ID=PRAENTID就是說上一條記錄的ID是本條記錄的PRAENTID,即本記錄的父親是上一條記錄。
COND3是過濾條件,用于對返回的所有記錄進(jìn)行過濾。
對于oracle進(jìn)行簡單oracle樹查詢(遞歸查詢)
DEPTID NUMBER 部門id
PAREDEPTID NUMBER 父部門id(所屬部門id)
NAME CHAR (40 Byte) 部門名稱
通過子節(jié)點(diǎn)向根節(jié)點(diǎn)追朔.
Ql代碼 select * from persons.dept start with deptid=76 connect by prior paredeptid=deptidselect * 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
通過根節(jié)點(diǎn)遍歷子節(jié)點(diǎn).
Ql代碼 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
select * from persons.dept start with paredeptid=0 connect by prior deptid=paredeptid
可通過level 關(guān)鍵字查詢所在層次.
Ql代碼 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
【編輯推薦】
教您如何刪除Oracle重復(fù)記錄
帶您了解Oracle層次查詢
帶您深入了解Oracle臨時(shí)表
oracle查詢用戶所有表的語句
Oracle with語句的用法
網(wǎng)站題目:oracle樹查詢的實(shí)現(xiàn)
文章來源:http://m.5511xx.com/article/djpigjg.html


咨詢
建站咨詢
