Oracle的外连接内连接和自连接
多表查询
等价连接:
两个表间的连接条件是通过 "=" 建立
别名: 一旦给表创建了别名,标识列时智能通过表的别名,而不能是表的真名
select * from emp e,dept d where e.deptno = d.deptno;
两个表间的连接条件不是通过"="号建立,可以是"<"、"between and"、以及"like"等 。
select * from emp e,dept d where e.deptno between d.deptno and d.deptno;
外连接
左外连接等同于在"="右边加 "+" ,将"="左边表中未匹配的记录也查找出来
右外连接等同于在"="左边加 "+" ,将"="右边表中未匹配的记录也查找出来
外连接只能出现在表达式的一边
包含外连接的条件不允许使用in操作符使用or操作符连接到另外一个条件
右外连接(right outer join) (将右边表中未匹配的记录查找出来)
select * from emp e,dept d where e.deptno(+) = d.deptno;
左外连接(left outer join) (将左边表中未匹配的内容查找出来)
select * from emp e,dept d where e.deptno = d.deptno(+);
内连接: inner join 即最常见的等值连接
select * from emp e,dept d where e.deptno = d.deptno;
全连接: full outer join 又称全外连接
即左右两表不匹配的都能查出来 关键字 full join on
select * from emp e full join dept d on e.deptno = d.deptno;
自连接:
自连接(self join)是使用自连接可以将自身表的一个镜像
当作另一个表来对待,
从而能够得到一些特殊的数据。
例如,每个员工都会有一个经理编号,我们可以通过自连接的方式将通过经理编号将名字查出来
没错,就是这样
select employ.ename "员工",manager.ename
from emp employ,emp manager
where employ.mgr = manager.empno;