-------------------外连接----------------------------
9i前:(+)
9i后:(+)、left outer join\right outer join\full outer join(ANIS标准.推荐)
例子:
查询出所有部门的雇员信息
select b.deptno,b.dname,a.* from emp a,dept b where a.deptno(+)=b.deptno;
select b.deptno,b.dname, a.* from emp a right join dept b on a.deptno=b.deptno;
select b.deptno,b.dname,a.* from dept b left join emp a on a.deptno=b.deptno;
*[(+)放在信息少的一边,join指向信息多的一边]
select b.deptno,count(empno) from emp a,dept b where a.deptno(+)=b.deptno group by b.deptno;
查询出所有部门的职位为经理的雇员数
select b.deptno,count(empno) from emp a,dept b where a.deptno(+)=b.deptno and a.job(+)='MANAGER' group by b.deptno; ---正确
select b.deptno,count(empno) from emp a,dept b where a.deptno(+)=b.deptno and a.job='MANAGER' group by b.deptno; ---错误
select b.deptno,count(empno) from emp a right join dept b on a.deptno=b.deptno and a.job='MANAGER' group by b.deptno; ---正确
select b.deptno,count(empno) from emp a right join dept b on a.deptno=b.deptno where a.job='MANAGER' group by b.deptno; ---错误
select b.deptno,count(empno) from emp a right join dept b on a.deptno=b.deptno where a.job(+)='MANAGER' group by b.deptno; ---错误
9i前:(+)
9i后:(+)、left outer join\right outer join\full outer join(ANIS标准.推荐)
例子:
查询出所有部门的雇员信息
select b.deptno,b.dname,a.* from emp a,dept b where a.deptno(+)=b.deptno;
select b.deptno,b.dname, a.* from emp a right join dept b on a.deptno=b.deptno;
select b.deptno,b.dname,a.* from dept b left join emp a on a.deptno=b.deptno;
*[(+)放在信息少的一边,join指向信息多的一边]
查询出每个部门的雇员数:
select b.deptno,count(empno) from emp a,dept b where a.deptno(+)=b.deptno group by b.deptno;select b.deptno,count(empno) from emp a,dept b where a.deptno(+)=b.deptno group by b.deptno;
查询出所有部门的职位为经理的雇员数
select b.deptno,count(empno) from emp a,dept b where a.deptno(+)=b.deptno and a.job(+)='MANAGER' group by b.deptno; ---正确
select b.deptno,count(empno) from emp a,dept b where a.deptno(+)=b.deptno and a.job='MANAGER' group by b.deptno; ---错误
select b.deptno,count(empno) from emp a right join dept b on a.deptno=b.deptno and a.job='MANAGER' group by b.deptno; ---正确
select b.deptno,count(empno) from emp a right join dept b on a.deptno=b.deptno where a.job='MANAGER' group by b.deptno; ---错误
select b.deptno,count(empno) from emp a right join dept b on a.deptno=b.deptno where a.job(+)='MANAGER' group by b.deptno; ---错误