oracle多表连接

时间:2015-05-28 19:45:14   收藏:0   阅读:382

Oracle表连接

SQL/Oracle使用表连接从多个表中查询数据

语法格式:

 

select 字段列表
from table1,table2
where table1.column1=table2.column2;

 

说明:

在where子句中指定连接条件

当被连接的多个表中存在同名字段时,必须在该字段前加上"表名"作为前缀.

连接的类型

Oracle8i之前的表连接:

等值连接(Equijoin)

非等值连接(Non-Equijoin)

外连接(Outer join):-->左外连接-->右外连接

自连接(Self join)

  

Oracle9之后新引入的连接形式(支持SQL99规范)

交叉连接(Cross join)

自然连接(Natural join)

使用Using子句建立连接

使用on子句建立连接

外连接(Outer join):-->左外连接-->右外连接-->全外连接

等值连接(Equijoin)

 

select empno,ename,sal,emp.deptno,dname from emp,dept
where emp.deptno = dept.deptno;

 

 多表连接中:

非等值连接(Non-Equijoin)

外连接(Outer join)

使用外连接可以看到参与连接的某一方不满足连接条件的记录

外连接运算符为(+)

传统的外连接分为左外连接和右外连接两种

语法格式:

 

select 字段列表
from table1,table2
where table1.column1(+)=table2.column2;
select 字段列表
from table1,table2
where table1.column1=table2.column2(+);

自连接(Self join)

 

 

select a.enpno,a.ename,a.ngr,b.ename
from emp a,emp b
where a.ngr = b.enpno;

 

 SQL99连接语法

SQL1999规范中规定的连接查询语法

 

技术分享
select 字段列表
from table1
[cross join table2] |
[natural join table2] |
[join table2 using(字段名)] |
[join table2 on(table.column_name=table2.column_name)] |
[(left | right | full out ) join table2
on(table1.column_name=table2.column_name)];
技术分享

 

交叉连接(Cross join)

Cross join 产生了一个笛卡尔集,其效果等同于再两个表进行连接时未使用where子句限定连接条件;

 

 

select empno,ename,sal,emp.deptno,dname
from emp cross join dept;

 

 

自然连接(Natural join)

Natural join基于两个表中的全部同名列建立连接

 

select empno,ename,sal,emp.deptno,dname
from emp natural join dept;

 

Using子句 

如果不希望参照被连接表的所有同名列进行等值连接,自然连接将无法满足要求,可以在连接时使用USING子句来设置用于等值连接的列(参照列)名.

 

select empno,ename,sal,emp.deptno,dname
from emp join dept
using(deptno);

 

不允许在参照列上使用表名或者别名作为前缀

On子句

如果要参照非同名的列进行等值连接,或想设置任意的连接条件,可以使用On子句

 

select empno,ename,sal,emp.deptno,dname
from emp join dept
on(emp.deptno=dept.deptno);

 

多表连接

使用SQL99连接语法,两个以上的表进行连接时应依次/分别指定相临的两个表之间的连接条件.

 

技术分享
select 字段列表
from table1
[cross join table2] |
[natural join table2] |
[join table2 using(字段名)] |
[join table2 on(table.column_name=table2.column_name)] |
[(left | right | full out ) join table2
on(table1.column_name=table2.column_name)]
[cross join table3] |
[natural join table3] |
[join table3 using(字段名)] |
[join table3 on(table.column_name=table3.column_name)] |
[(left | right | full out ) join table3
on(table2.column_name=table3.column_name)]...;
技术分享

 

内连接和外连接

内连接(Inner join)

在SQL99规范中,内连接只返回满足连接条件的数据.

外连接(Outer join)

左外连接(Left Outer Join)

两个表在连接过程中除返回满足连接条件的行为外,还返回左表中不满足条件的行为,这种连接称为左外连接.

右外连接(Right Outer Join)

两个表在连接过程中除返回满足连接条件的行为外,还返回右表中不满足条件的行为,这种连接称为右外连接.

满外连接(Full Outer Join)

Oracle9开始新增功能,两个表在连接过程中除返回满足连接条件的行为外,还返回两个表中不满足条件的所有行为,这种连接称为满外连接.

子查询(Sub Query)

子查询子查询在主查询前执行一次

主查询使用子查询的结果

 

select 字段列表
from table
where 表达式 operator (select 字段列表 from table);

 

使用子查询注意事项

单行子查询

单行子查询只返回一行记录

对单行子查询可使用单行记录比较运算符

=--------------等于

>--------------大于

>=------------大于等于

<--------------小于

<=--------------小于等于

<>--------------不等于

 

select * from emp
where sal>(select sal from emp where empno=7000);

 

子查询空值/多值问题

多行子查询

多行子查询返回多行记录

对多行子查询只能使用多行记录比较运算符

in--------------等于列表中的任何一个

any--------------和子查询返回的任意一个值比较

all--------------和子查询返回的所有值比较

 

select * from emp
where sal>any(select avg(sal) from emp group by deptno);
select * from emp
where sal>all(select avg(sal) from emp group by deptno);
select * from emp
where job in(select job from emp where ename=martin or ename=ssss);

 

TopN查询

在oracle中通常采用子查询的方式来实现Top n查询

 

技术分享
select 字段列表
from(select 字段列表 from table order by 排序字段)
where rownum <=n;
------------------------------------------------------------
select *
from(select * from emp order by sal desc)
where rownum <=5;
技术分享

原文:http://www.cnblogs.com/wenanbang/p/4536598.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!