有个空间

有个标题

Oracle 迭代查询

connect by prior start with 经常会被用到一个表中存在递归关系的时候。比如我们经常会将一个比较复杂的目录树存储到一个表中。或者将一些部门存储到一个表中,而这些部门互相有隶属关系。这个时候你就会用到connect by prior start with。

 表结构:

create table TB_ORG
(
  ORGID            VARCHAR2(50) not null,
  ORGNAME          VARCHAR2(100),
  ORGPARENT        VARCHAR2(50),
)

SQL> select orgid,ORGPARENT from tb_org order by ORGid desc;

ORGID                                              ORGPARENT
-----------------                                   --------------------------------------------------
top_org                                            
newsroom                                      top_org

sjgt                                                 newsroom
bgjsyj                                             newsroom
bgjs                                                newsroom
test52                                            bgjsyj
test36                                            bgjsyj
test3                                              bgjs
test2                                              sjgt
test1                                              sjgt
tes4t1                                             bgjs

--子取父
select * from tb_org CONNECT BY PRIOR orgparent=orgid START WITH orgid='newsroom'
-----子取父变形 ()
select * from tb_org CONNECT BY PRIOR orgparent=orgid START WITH orgparent='newsroom'
--父取子(结果中没有父这条记录)
select * from tb_org CONNECT BY PRIOR orgid=orgparent START WITH orgparent='newsroom'
-----父取子变形(多了父这一级)
select * from tb_org connect by prior orgid=orgparent start with orgid ='newsroom'

 

 

 

注意orgid=orgparent 和orgparent=orgid


ref:http://blog.csdn.net/kenny1985/article/details/3980583

posted on 2012-11-28 14:13 游雯 阅读(6468) 评论(0)  编辑  收藏 所属分类: Java技术