Posted on 2009-06-02 18:45
sailor 阅读(262)
评论(0) 编辑 收藏 所属分类:
oracle
DEPTID PAREDEPTID NAME
NUMBER NUMBER CHAR (40 Byte)
部门id 父部门id(所属部门id) 部门名称
通过子节点向根节点追朔。
select * from persons.dept start with deptid=76 connect by prior paredeptid=deptid
通过根节点遍历子节点。
select * from persons.dept start with paredeptid=0 connect by prior deptid=paredeptid
可通过level 关键字查询所在层次。
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 后面所放的字段是有关系的,它指明了查询的方向。
----------------------------------------------------------------------------------
今天用了此语句,结果冗余了,语句如下:
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联合使用,如果要用where限定条件就把它当做子表来处理吧