一个开发组同事在对旧系统代码进行分析的时候,发现(+)连接语句。这里想说说(+)连接和Join连接的使用。
(+)连接是Oracle一种特有的连接方法,和我们常见的Join系列连接同时使用。(+)连接是一种外连接方式,作为简单连接的一种拓展类型。我们通常理解的连接,是只将符合连接条件的数据集合返回来。而用外连接之后,可以连带将特定数据表的不符合条件行一并返回。
Join系列连接的特点是有专门的连接对象和被连接对象,使用on设置专门的连接条件。而(+)连接的连接条件是放置在where条件列表中的。下面我们通过一些实验,来展示两者的差异和特征。
1、 环境准备
我们模拟一个简单的实验环境。
SQL> select * from dept;
DEPTNO DEPTNAME
---------- ----------
1 dept1
2 dept2
3 dept3
SQL> select * from emp;
EMPNO EMPNAME DEPTNO
---------- -------------------- ----------
1 emp1 1
2 emp2 1
3 emp3 2
4 emp4 4
两个数据表是使用deptno进行连接,两边分别存在不匹配的内容。如果我们使用最常见的连接方式,结果明显是严格的连接匹配。
SQL> select * from dept, emp where dept.deptno=emp.deptno;
DEPTNO DEPTNAME EMPNO EMPNAME DEPTNO
---------- ---------- ---------- -------------------- ----------
1 dept1 1 emp1 1
1 dept1 2 emp2 1
2 dept2 3 emp3 2
2、(+)连接实验
(+)连接语句是加入在where条件后的部分,作为查询条件的一部分。
SQL> select * from dept, emp where dept.deptno(+)=emp.deptno;
DEPTNO DEPTNAME EMPNO EMPNAME DEPTNO
---------- ---------- ---------- -------------------- ----------
1 dept1 2 emp2 1
1 dept1 1 emp1 1
2 dept2 3 emp3 2
4 emp4 4
SQL> select * from dept, emp where dept.deptno=emp.deptno(+);
DEPTNO DEPTNAME EMPNO EMPNAME DEPTNO
---------- ---------- ---------- -------------------- ----------
1 dept1 1 emp1 1
1 dept1 2 emp2 1
2 dept2 3 emp3 2
3 dept3
两个例子可以告诉我们(+)使用的特点,(+)连接的结果是将(+)号对面的数据表不符合记录的结果也输出出来。
(+)连接实质上是一种外连接Outer Join的现象,通过不同的位置来对应不同方向的结果。
3、 Join系列连接
我们经常使用的Join功能要强大的多,而且可读性好一些。Join的特征是将连接条件通过专门的on子句进行说明,而不是放置在where子句里面。
ü 左连接Join
左连接left join是保证from子句中的数据表全部显示。
SQL> select * from dept
2 left join emp
3 on dept.deptno=emp.deptno;
DEPTNO DEPTNAME EMPNO EMPNAME DEPTNO
---------- ---------- ---------- -------------------- ----------
1 dept1 1 emp1 1
1 dept1 2 emp2 1
2 dept2 3 emp3 2
3 dept3
ü 右连接Join
右连接right join是保证Join的数据表全部显示。对该子句笔者认为可以变通为left join,这样似乎更容易看懂。
SQL> select * from dept
2 right join emp
3 on dept.deptno=emp.deptno;
DEPTNO DEPTNAME EMPNO EMPNAME DEPTNO
---------- ---------- ---------- -------------------- ----------
1 dept1 2 emp2 1
1 dept1 1 emp1 1
2 dept2 3 emp3 2
4 emp4 4
ü 全连接full join
全连接是将两个数据表中符合连接条件或者不符合连接条件的全部列出。注意:不符合连接条件的结果是不会进行链接的。
SQL> select * from dept
2 full join emp
3 on dept.deptno=emp.deptno;
DEPTNO DEPTNAME EMPNO EMPNAME DEPTNO
---------- ---------- ---------- -------------------- ----------
1 dept1 1 emp1 1
1 dept1 2 emp2 1
2 dept2 3 emp3 2
4 emp4 4
3 dept3
ü 内连接inner join
内连接就类似与我们传统where的连接方式了,返回严格符合条件的数据列。
SQL> select * from dept
2 inner join emp
3 on dept.deptno=emp.deptno;
DEPTNO DEPTNAME EMPNO EMPNAME DEPTNO
---------- ---------- ---------- -------------------- ----------
1 dept1 1 emp1 1
1 dept1 2 emp2 1
2 dept2 3 emp3 2
ü 笛卡尔积连接Cross Join
Cross Join是进行全匹配,两两之间进行笛卡尔积运算。
SQL> select * from dept
2 cross join emp
3 ;
DEPTNO DEPTNAME EMPNO EMPNAME DEPTNO
---------- ---------- ---------- -------------------- ----------
1 dept1 1 emp1 1
1 dept1 2 emp2 1
1 dept1 3 emp3 2
1 dept1 4 emp4 4
2 dept2 1 emp1 1
2 dept2 2 emp2 1
2 dept2 3 emp3 2
2 dept2 4 emp4 4
3 dept3 1 emp1 1
3 dept3 2 emp2 1
3 dept3 3 emp3 2
3 dept3 4 emp4 4
12 rows selected
SQL> select * from dept
2 cross join emp
3 where dept.deptno=emp.deptno;
DEPTNO DEPTNAME EMPNO EMPNAME DEPTNO
---------- ---------- ---------- -------------------- ----------
1 dept1 1 emp1 1
1 dept1 2 emp2 1
2 dept2 3 emp3 2
4、 结论
(+)连接是Oracle中经常使用的一种连接方法语法,从简单可读的角度看,似乎要比Join系列要好一些。了解不同类型的Join特征,才能更好的获取到希望的数据,用好Oracle数据库的强大功能。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/17203031/viewspace-703074/ ,如需转载,请注明出处,否则将追究法律责任。