Oracle inner join、left join、right join

Oracle inner join、left join、right join 、+左边或者右边的区别

我们以Oracle自带的表来做例子

主要两张表:dept、emp

一个是部门,一个是员工表结构如下:

namenull?Type
Empnonot nullnumber(4)
ename varchar2(10)
job varchar2(9)
mgr number(4)
hiredate date
sal number(7,2)
comm number(7,2)
deptno number(2)
namenull?Type
deptnonot nullnumber(2)
dname varchar2(14)
log varchar2(13)

这两张表 dept是主表 emp是子表,关联的列是deptnodept表现有数据

   emp表现有数据 

inner join 意思是内连接 把匹配的信息全部查出来SQL>select e.empno,e.ename,e.job,d.deptno,d.dname 
from emp e innerjoin dept d on e.deptno=d.deptno orderby e.empno;查询的结果:

 left join 左连接 意思是包含左边表所有记录,右边所有的匹配的记录,如果没有则用空补齐SQL>select e.empno,e.ename,e.job,d.deptno,d.dname 
from emp e leftjoin dept d on e.deptno=d.deptno orderby e.empno;解释SQL :这条SQL语句左边是EMP表 左连接结果查询出EMP所有的记录,然后根据左边表匹配出右边表DEPT所有的记录 查询结果如下: 

right join 右连接  意思是包括右边表所有记录,匹配左边表的记录,如果没有则以空补齐
SQL>select e.empno,e.ename,e.job,d.deptno,d.dname
from emp e rightjoin dept d on e.deptno=d.deptno orderby e.empno;解释SQL:这条SQL语句EMP在左边,DEPT在右边,然后我们采用右连接,就查出右边表所有的数据 查询结构: full join  全连接 意思是左右表所有的记录全部显示出来SQL>select e.empno,e.ename,e.job,d.deptno,d.dname
from emp e fulljoin dept d on e.deptno=d.deptno orderby e.empno;
解释SQL:这条SQL语句采用全连接,查询出左表和右表所有的值出来查询结果:

  介绍Oracle +连接方式及说明 +在左边 为右连接SQL>select e.empno,e.ename,e.job,d.deptno,d.dname 
from emp e,dept d where e.deptno(+)=d.deptno orderby e.empno; 
查询结果: 

+在右边 为左连接SQL>select e.empno,e.ename,e.job,d.deptno,d.dname from emp e,dept d where e.deptno=d.deptno(+) orderby e.empno; 

查询结果: 

Oracle 内连接(inner join)、外连接(outer join)、全连接(full join)

之前没有用过 full outer join,第一次用,学习一下

Student表

Color表

Oracle中的连接可分为,内连接(inner join)、外连接(outer join)、全连接(full join),不光是Oracle,其他很多的数据库也都有这3种连接查询方式:

内连接inner join/join

也叫自连接,这是我们经常用到的查询方式,内连接查询只能查询出匹配的记录,匹配不上的记录时无法查询出来的 ,以下三种查询结果一样

select * from student s, color c where s.stuname = c.stuname;

select * from student s inner join color c on s.stuname = c.stuname;

select * from student s join color c on s.stuname = c.stuname;

外连接outer join

可进一步分为左外连接left outer join和右外连接right outer join,(简称左连接left join,右连接 right join)。

左外连接

左连接就是以左边的表(left join 左边的表)为主表,即使有些记录关联不上,主表的信息也能全部查询出来,也就是左边的表数据全部展示,右边表的数据复合条件的展示,不符合条件的以空值代替,适合那种需要求出维度(比如求出所有人员)的需求:

select * from student s left join color c on s.stuname = c.stuname;

等同于select * from student s left outer join color c on s.stuname = c.stuname;

右外连接

如果有需求要求在结果中展现所有的颜色信息,就可以用右连接:

还有另一种写法,可以达到相同的外连接效果:比如左外连接等同于以下的语句:

select * from student s ,color c where s.stuname = c.stuname(+);

同样右连接是这样的:

select * from student s ,color c where s.stuname(+) = c.stuname;

在(+)计算时,哪个带(+)哪个需要条件符合的,另一个全部的。即放左即右连接,放右即左连接。

全连接full join/full outer join

语法是语法为full join … on …,全连接的查询结果是左外连接和右外连接查询结果的并集,即使一些记录关联不上,也能够把部分信息查询出来:

产生M+N的结果集,列出两表全部的,不符合条件的,以空值代替。

select * from student s full join color c on s.stuname = c.stuname;

select * from student s full join color c on 1=1

笛卡尔乘积cross join

即不加任何条件,达到 M*N 的结果集。

以下两种查询结果一样。

select * from student s cross join color c

select * from student s , color c

注意:如果cross join加上where on s.stuname = c.stuname条件,会产生跟自连接一样的结果(cross join 后加上 on 报错):

加上条件,产生跟自连接一样的结果。

select * from student s cross join color c where s.stuname = c.stuname;

自连接结果集的cross join连接结果

总结

ü   所有的join连接,都可以加上类似where a.id=’1000’的条件,达到同样的效果。因为on不能做这种判断,只能是

ü   除了cross join不可以加on外,其它join连接都必须加上on关键字,后都可加where条件。

ü   虽然都可以加where条件,但是他们只在标准连接的结果集上查找where条件。比如左外连接的结果没有class的三班,所以如果加 where class.id=’C003’虽然在表中有,但在左连接结果集中没有,所以查询后,是没有记录的。

A表有100条数据,B表有80条数据,left join on 1=1,where 1=1的结果是:

应该是笛卡尔积

A表有100条数据,B表有80条数据,left join on 1=2,where 1=1的结果是:

应该是A表的值

测试:

select * from student s left join color c on 1=1 where 1=1

select * from student s left join color c on 1=2 where 1=1