Posted on 2009-06-02 18:45
sailor 閱讀(265)
評論(0) 編輯 收藏 所屬分類:
oracle
DEPTID PAREDEPTID NAME
NUMBER NUMBER CHAR (40 Byte)
部門id 父部門id(所屬部門id) 部門名稱
通過子節(jié)點向根節(jié)點追朔。
select * from persons.dept start with deptid=76 connect by prior paredeptid=deptid
通過根節(jié)點遍歷子節(jié)點。
select * from persons.dept start with paredeptid=0 connect by prior deptid=paredeptid
可通過level 關(guān)鍵字查詢所在層次。
select a.*,level from persons.dept a start with paredeptid=0 connect by prior deptid=paredeptid
再次復習一下:start with ……connect by 的用法, start with 后面所跟的就是就是遞歸的種子。
遞歸的種子也就是遞歸開始的地方 connect by 后面的"prior" 如果缺省:則只能查詢到符合條件的起始行,并不進行遞歸查詢;
connect by prior 后面所放的字段是有關(guān)系的,它指明了查詢的方向。
----------------------------------------------------------------------------------
今天用了此語句,結(jié)果冗余了,語句如下:
select CATEGORYTRANS_ID, bbj.category_id,bbj.PARENT_ID, obj.category_name from n_Categorytrans obj,n_Category bbj
where obj.category_id = bbj.category_id and obj.cur_language ='zh'
and bbj.company_id =924
start with bbj.parent_id =1201
connect by prior bbj.category_id=parent_id
改成如下語句就對了:
select * from ( select CATEGORYTRANS_ID, bbj.category_id,bbj.PARENT_ID, obj.category_name from n_Categorytrans obj,n_Category bbj
where obj.category_id = bbj.category_id and obj.cur_language ='zh'
and bbj.company_id =924)
start with parent_id =1201
connect by prior category_id=parent_id
------------------------------------------------------
好像是此語句只是單獨使用,不能用where聯(lián)合使用,如果要用where限定條件就把它當做子表來處理吧