时间:2021-07-01 10:21:17 帮助过:2人阅读
外连接, 居于次要的那一方, 无论什么条件筛选,记得加上(+), 否则会导致数据漏掉, 实际查到的就变成了 内连接!!!
*******************************下面是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
标签: