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

oracle中的select語(yǔ)句可以用START WITH...CONNECT BY PRIOR子句實(shí)現(xiàn)遞歸查詢,connect by 是結(jié)構(gòu)化查詢中用到的,其基本語(yǔ)法是:
select * from tablename start with cond1
connect by cond2
where cond3;
簡(jiǎn)單說(shuō)來(lái)是將一個(gè)樹(shù)狀結(jié)構(gòu)存儲(chǔ)在一張表里,比如一個(gè)表中存在兩個(gè)字段:
id,parentid那么通過(guò)表示每一條記錄的parent是誰(shuí),就可以形成一個(gè)樹(shù)狀結(jié)構(gòu)。
用上述語(yǔ)法的查詢可以取得這棵樹(shù)的所有記錄。
其中COND1是根結(jié)點(diǎn)的限定語(yǔ)句,當(dāng)然可以放寬限定條件,以取得多個(gè)根結(jié)點(diǎn),實(shí)際就是多棵樹(shù)。
COND2是連接條件,其中用PRIOR表示上一條記錄,比如 CONNECT BY PRIOR ID=PRAENTID就是說(shuō)上一條記錄的ID是本條記錄的PRAENTID,即本記錄的父親是上一條記錄。
COND3是過(guò)濾條件,用于對(duì)返回的所有記錄進(jìn)行過(guò)濾。
對(duì)于oracle進(jìn)行簡(jiǎn)單oracle樹(shù)查詢(遞歸查詢)
DEPTID NUMBER 部門id
PAREDEPTID NUMBER 父部門id(所屬部門id)
NAME CHAR (40 Byte) 部門名稱
通過(guò)子節(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
通過(guò)根節(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
可通過(guò)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查詢用戶所有表的語(yǔ)句
Oracle with語(yǔ)句的用法
新聞標(biāo)題:oracle樹(shù)查詢的實(shí)現(xiàn)
文章鏈接:http://www.5511xx.com/article/djpigjg.html


咨詢
建站咨詢
