左连接,右连接,内连接,外连接, join, left join, right join ,mysql ,oracle

Posted notfresh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了左连接,右连接,内连接,外连接, join, left join, right join ,mysql ,oracle相关的知识,希望对你有一定的参考价值。

2016-6-12 22:35:51

工作用了一年多的oracle,最近在学mysql, 仔细想想 各种连接,感觉这些概念还是蛮烦人的! 最近整理了一下,分享一下自己的理解,有些东西是借鉴网上并自己吸收了的.

 

1.不管是什么连接,oracle和mysql的原理是一模一样的,只是有些写法不一样而已.说到写法,这里提一下,

select * from A, B where a.filed1=b.filed2; --这是第1种写法, 内连接,这样写,很方便, oracle和mysql通用

select * from A join B on A.filed1=b.field2 ;  --这是第2种写法, 内连接,这样写,不是很方便, 老鸟都不会这么写的 , oracle和mysql通用,

内连接写法一样,但是 外连接, oracle 有自己额外特有的写法, mysql只能用通用的写法了...

 

2.几个概念

左外连接=左连接

右连接= 右外连接

全连接, 不说,太简单了.
内连接,平时用的最多, 最简单的等于号连接
举例子:
有两个表,  
 假设A和B是多对一的关系 
A   left   join   B   的连接的记录数与A表的记录数同  
技术分享 A   right   join  B  的连接的记录数 等于 A和B内连接的记录数 加上 B没有匹配上的记录条数    
 这样,就能很好的理解 左连接和右连接了
A   left   join   B   等价 B   right   join   A  .
 
附上测试的脚本,以最经典的 oracle的scott用户下的 部门员工表为例子吧!
ORALCE脚本--在SQL window里跑

create table EMP
(
empno NUMBER(4) not null,
ename VARCHAR2(10),
job VARCHAR2(9),
mgr NUMBER(4),
hiredate DATE,
sal NUMBER(7,2),
comm NUMBER(7,2),
deptno NUMBER(2),
desc2 VARCHAR2(30) default 2
)
;
-- Create/Recreate primary, unique and foreign key constraints
alter table EMP
add constraint PK_EMP primary key (EMPNO);
alter table EMP
add constraint FK_DEPTNO foreign key (DEPTNO)
references DEPT (DEPTNO);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DESC2)
values (7369, ‘SMITH‘, ‘CLERK‘, 7902, to_date(‘17-12-1980‘, ‘dd-mm-yyyy‘), 800.00, 1999.00, 20, null);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DESC2)
values (7499, ‘ALLEN‘, ‘SALESMAN‘, 7698, to_date(‘20-02-1981‘, ‘dd-mm-yyyy‘), 1600.00, 300.00, 30, null);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DESC2)
values (7521, ‘WARD‘, ‘SALESMAN‘, 7698, to_date(‘22-02-1981‘, ‘dd-mm-yyyy‘), 1250.00, 500.00, 30, null);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DESC2)
values (7566, ‘JONES‘, ‘MANAGER‘, 7839, to_date(‘02-04-1981‘, ‘dd-mm-yyyy‘), 2975.00, null, 20, null);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DESC2)
values (7654, ‘MARTIN‘, ‘SALESMAN‘, 7698, to_date(‘28-09-1981‘, ‘dd-mm-yyyy‘), 1250.00, 1400.00, 30, null);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DESC2)
values (7698, ‘BLAKE‘, ‘MANAGER‘, 7839, to_date(‘01-05-1981‘, ‘dd-mm-yyyy‘), 2850.00, null, 30, null);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DESC2)
values (7782, ‘CLARK‘, ‘MANAGER‘, 7839, to_date(‘09-06-1981‘, ‘dd-mm-yyyy‘), 2450.00, null, 10, null);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DESC2)
values (7788, ‘SCOTT‘, ‘ANALYST‘, 7566, to_date(‘19-04-1987‘, ‘dd-mm-yyyy‘), 3000.00, null, 20, null);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DESC2)
values (7839, ‘KING‘, ‘PRESIDENT‘, null, to_date(‘17-11-1981‘, ‘dd-mm-yyyy‘), 5000.00, null, 10, null);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DESC2)
values (7844, ‘TURNER‘, ‘SALESMAN‘, 7698, to_date(‘08-09-1981‘, ‘dd-mm-yyyy‘), 1500.00, 0.00, 30, null);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DESC2)
values (7876, ‘ADAMS‘, ‘CLERK‘, 7788, to_date(‘23-05-1987‘, ‘dd-mm-yyyy‘), 1100.00, null, 20, null);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DESC2)
values (7900, ‘JAMES‘, ‘CLERK‘, 7698, to_date(‘03-12-1981‘, ‘dd-mm-yyyy‘), 950.00, null, 30, null);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DESC2)
values (7902, ‘FORD‘, ‘ANALYST‘, 7566, to_date(‘03-12-1981‘, ‘dd-mm-yyyy‘), 3000.00, null, 20, null);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DESC2)
values (7934, ‘MILLER‘, ‘CLERK‘, 7782, to_date(‘23-01-1982‘, ‘dd-mm-yyyy‘), 1300.00, null, 10, null);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DESC2)
values (7935, ‘KATE‘, ‘MANAGER‘, null, null, null, null, null, null);

部门 表

-- Create table
create table DEPT
(
deptno NUMBER(2) not null,
dname VARCHAR2(14),
loc VARCHAR2(13)
)
;
-- Create/Recreate primary, unique and foreign key constraints
alter table DEPT
add constraint PK_DEPT primary key (DEPTNO);

insert into dept (DEPTNO, DNAME, LOC)
values (10, ‘ACCOUNTING‘, ‘NEW YORK‘);

insert into dept (DEPTNO, DNAME, LOC)
values (20, ‘RESEARCH‘, ‘DALLAS‘);

insert into dept (DEPTNO, DNAME, LOC)
values (30, ‘SALES‘, ‘CHICAGO‘);

insert into dept (DEPTNO, DNAME, LOC)
values (40, ‘OPERATIONS‘, ‘BOSTON‘);

 测试语句如下:

select * from emp e,dept d where e.deptno(+)=d.deptno;--oracle 特有的右连接写法
select * from emp e,dept d where e.deptno=d.deptno(+);--oracle 特有的左连接写法


select * from emp e,dept d where e.deptno=d.deptno; --内连接,又叫自然连接
select * from emp e full join dept d on e.deptno=d.deptno; --全连接 , full join

 

再说下oracle外连接的时候的一个注意事项!

select * from emp e,dept d where e.deptno(+)=d.deptno and e.empno(+) like ‘78%‘;

外连接, 居于次要的那一方, 无论什么条件筛选,记得加上(+), 否则会导致数据漏掉, 实际查到的就变成了 内连接!!!

 

 

*******************************下面是mysql 脚本和测试语句 *****************************************************************************************

emp表

create table emp (
empno varchar (12),
ename varchar (30),
job varchar (27),
mgr varchar (12),
hiredate date ,
sal varchar (21),
comm varchar (21),
deptno varchar (6)
);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values(‘7369‘,‘SMITH‘,‘CLERK‘,‘7902‘,‘1980-12-17‘,‘800‘,NULL,‘20‘);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values(‘7499‘,‘ALLEN‘,‘SALESMAN‘,‘7698‘,‘1981-02-20‘,‘1600‘,‘300‘,‘30‘);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values(‘7521‘,‘WARD‘,‘SALESMAN‘,‘7698‘,‘1981-02-22‘,‘1250‘,‘500‘,‘30‘);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values(‘7566‘,‘JONES‘,‘MANAGER‘,‘7839‘,‘1981-04-02‘,‘2975‘,NULL,‘20‘);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values(‘7654‘,‘MARTIN‘,‘SALESMAN‘,‘7698‘,‘1981-09-28‘,‘1250‘,‘1400‘,‘30‘);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values(‘7698‘,‘BLAKE‘,‘MANAGER‘,‘7839‘,‘1981-05-01‘,‘2850‘,NULL,‘30‘);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values(‘7782‘,‘CLARK‘,‘MANAGER‘,‘7839‘,‘1981-06-09‘,‘2450‘,NULL,‘10‘);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values(‘7788‘,‘SCOTT‘,‘ANALYST‘,‘7566‘,‘1987-04-19‘,‘3000‘,NULL,‘20‘);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values(‘7839‘,‘KING‘,‘PRESIDENT‘,NULL,‘1981-11-17‘,‘5000‘,NULL,‘10‘);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values(‘7844‘,‘TURNER‘,‘SALESMAN‘,‘7698‘,‘1981-09-08‘,‘1500‘,‘0‘,‘30‘);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values(‘7876‘,‘ADAMS‘,‘CLERK‘,‘7788‘,‘1987-05-23‘,‘1100‘,NULL,‘20‘);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values(‘7900‘,‘JAMES‘,‘CLERK‘,‘7698‘,‘1981-12-03‘,‘950‘,NULL,‘30‘);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values(‘7902‘,‘FORD‘,‘ANALYST‘,‘7566‘,‘1981-12-03‘,‘3000‘,NULL,‘20‘);
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values(‘7934‘,‘MILLER‘,‘CLERK‘,‘7782‘,‘1982-01-23‘,‘1300‘,NULL,‘10‘);

 

create table dept (
deptno varchar (6),
dname varchar (42),
loc varchar (39)
);
insert into dept (deptno, dname, loc) values(‘50‘,‘MARKETING‘,‘CHONGQING‘);
insert into dept (deptno, dname, loc) values(‘10‘,‘ACCOUNTING‘,‘NEW YORK‘);
insert into dept (deptno, dname, loc) values(‘20‘,‘RESEARCH‘,‘DALLAS‘);
insert into dept (deptno, dname, loc) values(‘30‘,‘SALES‘,‘CHICAGO2‘);
insert into dept (deptno, dname, loc) values(‘40‘,‘OPERATIONS‘,‘BOSTON2‘);

-- 我自己写的location 表,为了测试更复杂的3个表 左连接,右连接!

create table location (
locNo varchar (30),
locDesc varchar (90)
);
insert into location (locNo, locDesc) values(‘NEW YORK‘,‘纽约‘);
insert into location (locNo, locDesc) values(‘DALLAS‘,‘达拉斯‘);
insert into location (locNo, locDesc) values(‘CHICAGO‘,‘芝加哥‘);
insert into location (locNo, locDesc) values(‘BOSTON‘,‘波士顿‘);
insert into location (locNo, locDesc) values(‘BEIJING‘,‘北京‘);
insert into location (locNo, locDesc) values(‘SHANGHAI‘,‘上海‘);
insert into location (locNo, locDesc) values(‘XIAN‘,‘西安‘);

 

测试语句:

select
*
from
dept d
right join emp e
on d.deptno = e.`deptno`
left join location l
on d.`loc` = l.`locNo`
order by d.`deptno` ;

 

select
*
from
dept d
right join emp e
on d.deptno = e.`deptno`
right join location l
on d.`loc` = l.`locNo`
order by d.`deptno` ;

 

select 

from
dept d 
left join emp e
on d.deptno = e.`deptno` 
right join location l 
on d.`loc` = l.`locNo` 
order by d.`deptno` ;

 

通过以上三个语句测试发现,   

继续以A和B举例:

 A   left   join   B  on a.field1=B.field2   , field1列的值 全部都有, 
 A  right   join   B  on a.field1=B.field2   , field2列的值 全部都有, 
连接的数 为内连接的数 + 主连接的那一列 多余的行数 
 
左连接,以左连接那一列值集合为准!! 右连接,同理!
 
有多个外连接,按从左到右的顺序连接
 
最后这几句表达不是很准确,但大致是这个意思,谅解.

 

以上就是我的个人理解,希望能帮到你们,同时有错误,请指正!

以上是关于左连接,右连接,内连接,外连接, join, left join, right join ,mysql ,oracle的主要内容,如果未能解决你的问题,请参考以下文章

Hive sql中的 各种join(内连接左外连接右外连接满外连接)

Hive sql中的 各种join(内连接左外连接右外连接满外连接)

左连接,右连接,内连接,外连接, join, left join, right join ,mysql ,oracle

内连接,外链接(左连接右连接全连接),交叉连接大总结+附SQL JOINS图解[转]

SQL——左连接(Left join)右连接(Right join)内连接(Inner join)

Oracle表与表之间的连接方式(内连接:inner join 外连接 全连接: full outer join左连接:left outer join 右连接:right outer join(代码