Oracle 迭代查询 CONNECT BY PRIOR ……START WITH ……
2014-03-24 11:15
555 查看
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
转自:http://blog.csdn.net/kenny1985/article/details/3980583
表结构:
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
转自:http://blog.csdn.net/kenny1985/article/details/3980583
相关文章推荐
- Oracle 迭代查询 CONNECT BY PRIOR ……START WITH ……
- oracle树形结构层级查询之start with ....connect by prior、level、order by以及sys_connect_by_path之浅谈
- mysql的逆袭:如何做oracle的start with connect by prior递归层次查询
- ORACLE查询树型关系(connect by prior start with) .
- ORACLE查询树型关系(connect by prior start with)
- ORACLE 层次结构查询语句 start with connect by prior
- ORACLE查询树型关系(start with connect by prior)
- 【转】ORACLE查询树型关系(start with connect by prior)
- ORACLE查询树型关系(start with connect by prior)
- MySql的sql语句中添加存储过程或者存储函数来实现Oracle中的start with ……connect by prior……递归(树形结构数据)查询
- ORACLE查询树型关系(connect by prior start with)
- Oracle实现树查询:select ... start with ... connect by prior
- ORACLE树查询,start with connect by prior
- ORACLE查询树型关系(connect by prior start with)
- oracle递归层级查询 start with connect by prior
- ORACLE查询树型关系(connect by prior start with)
- oracle树结构查询(start with connect by prior )
- ORACLE查询树型关系(connect by prior start with)
- ORACLE树查询,start with ... connect by prior ...
- Oracle查询树型关系(start with connect by prior)