oracle中的select ...connect by prior ...start with及(+)的用法
oracle中的select ...connect by prior ...start with及(+)的用法
1.select ...connect by prior ...start with的用法:
select ... from <tablename>
where <conditional-1> --过滤条件,用于对返回的所有记录进行过滤
start with <conditional-2> --查询结果从起始根节点的限定条件
connect by prior <conditional-3> --连接条件
数据库表结构如下:
create table tablename
(
id number,
root_id number,
name varchar2(50),
desc varchar2(250)
)
insert into tablename(id,root_id,name,desc) values(1,0,'root','根节点');
insert into tablename(id,root_id,name,desc) values(2,1,'childnode1','子节点1');
insert into tablename(id,root_id,name,desc) values(3,1,'childnode2','子节点2');
insert into tablename(id,root_id,name,desc) values(4,0,'root2','根节点2');
insert into tablename(id,root_id,name,desc) values(5,4,'childnodeA','子节点1');
insert into tablename(id,root_id,name,desc) values(6,4,'childnodeB','子节点2');
得到完整树:
select * from tablename connect by prior id=root_id start with root_id=0
如果connect by prior 中的prior被省略,则查询将不进行深层递归。
如:
select * from tablename connect by id=root_id start with root_id=0
2.oracle中(+) (OUTER JOIN)的用法
SELECT A.id, B.IDD FROM A, B WHERE A.id(+)=B.IDD
等价于
SELECT A.id, B.IDD FROM A RIGHT OUTER JOIN B ON ( A.id=B.IDD)
意思是 两表关联的时候,
B 表的数据, 全部检索出来。
A表的数据,则是在B表有相对应的数据的情况下,才检索出来。