内连接:
以下两个语法实现的结果相同
Select * from G3E_ATTRIBUTE A JOIN G3E_TABATTRIBUTE B ON A.G3E_ANO=B.G3E_ANO
SELECT * FROM G3E_ATTRIBUTE A,G3E_TABATTRIBUTE B WHERE A.G3E_ANO=B.G3E_ANO
外连接:外连接分为两种,一种是左连接(Left JOIN)和右连接(Right JOIN)
(1)左连接(Left JOIN)表示左边的表全部显示,右边不匹配的用空的表示
语句如下:select * from A Left JOIN B ON A.Aid=B.Bnameid
select * from A ,B where A.Aid=B.Bnameid(+)
(2)右连接(Right JOIN)表示右边的表全部显示,左边不匹配的用空的表示
语句如下:select * from A Right JOIN B ON A.Aid=B.Bnameid 右连接
select * from A ,B where A.Aid(+)=B.Bnameid 右连接
合并查询
为了合并多个select语句的结果,可以时也能够集合操作符UNION,UNION ALL,INTERSECT和MINUS。
1、UNION操作符用于获取两个结果的并集。当使用该操作符时,会自动去掉结果集中的重复行,并且会以第一列的结果进行排序。
select g3e_ano,g3e_username,g3e_field from g3e_attribute where g3e_ano>5000
union
select g3e_ano,g3e_username,g3e_field from g3e_attribute where g3e_username like '%光缆%';
2、UNION ALL操作符用于获取两个结果集的并集。但是与UNION操作符不同的是,该操作符不会取消重复值,而且也不会以任何列进行排序。
select g3e_ano,g3e_username,g3e_field from g3e_attribute where g3e_ano>5000
union all
select g3e_ano,g3e_username,g3e_field from g3e_attribute where g3e_username like '%光缆%';
3、INTERSECT操作符用于获取两个结果集的交集。当使用该操作符时,只会显示同时存在于两个结果集中的数据,并且会以第一列进行排序。
select g3e_ano,g3e_username,g3e_field from g3e_attribute where g3e_ano>5000
intersect
select g3e_ano,g3e_username,g3e_field from g3e_attribute where g3e_username like '%光缆%';
4、MINUS操作符用于获取两个结果集的差集。当使用该操作符时,只会显示在第一个结果集中存在,在第二个结果集中不存在的数据,并且会以第一列进行排序。
select g3e_ano,g3e_username,g3e_field from g3e_attribute where g3e_ano>5000
minus
select g3e_ano,g3e_username,g3e_field from g3e_attribute where g3e_username like '%光缆%';
层次查询
来源于http://smmt315.blog.163.com/blog/static/1363980782010084471760/
create table Child_Parent (child number,parent number);
insert into Child_Parent (CHILD, PARENT)
values (2, 5);
insert into Child_Parent (CHILD, PARENT)
values (3, 5);
insert into Child_Parent (CHILD, PARENT)
values (10, 15);
insert into Child_Parent (CHILD, PARENT)
values (5, 15);
insert into Child_Parent (CHILD, PARENT)
values (9, 17);
insert into Child_Parent (CHILD, PARENT)
values (8, 17);
insert into Child_Parent (CHILD, PARENT)
values (15, 38);
insert into Child_Parent (CHILD, PARENT)
values (17, 38);
insert into Child_Parent (CHILD, PARENT)
values (6, 38);
insert into Child_Parent (CHILD, PARENT)
values (13, 26);
insert into Child_Parent (CHILD, PARENT)
values (1, 26);
insert into Child_Parent (CHILD, PARENT)
values (12, 26);
insert into Child_Parent (CHILD, PARENT)
values (11, 18);
insert into Child_Parent (CHILD, PARENT)
values (7, 18);
insert into Child_Parent (CHILD, PARENT)
values (38, null);
insert into Child_Parent (CHILD, PARENT)
values (26, null);
insert into Child_Parent (CHILD, PARENT)
values (18, null);
commit;
select * from Child_Parent
select a.child,
a.parent,
level "层次",
sys_connect_by_path(child, '->') "合并层次",
prior a.child "父节点",
connect_by_root a.child "根节点",
decode(connect_by_isleaf, 1, a.child, null) "子节点",
decode(connect_by_isleaf, 1, '是', '否') "是否子节点"
from Child_Parent a
start with a.parent is null --从parent为空开始扫描
connect by a.parent = prior a.child --以child为父列连接parent
order siblings by child desc; --对层次排序
其中伪列LEVEL用于返回层次结构的层次(1:根行;2:第二级行;3:第三级行……);
START WITH 子句用于指定层次查询的根行;
CONNECT BY 子句用于执行父行和子行之间的关系,
当定义父行和子行的关系时,必须使用[PRIOR]关键字,并且column1和column2对应于父键列或者子键列。