彻底搞懂Oracle的左外连接和右外连接(以数据说话)
url: http://blog.csdn.net/liuxiit/article/details/5791402
Employees表:
Department表:
select e.last_name,e.department_id,d.department_name
from employees e
left outer join departments d
on (e.department_id = d.department_id);
select e.last_name, d.department_name
from employees e, departments d
where e.department_id = d.department_id(+) 左外连接
结果为:所有员工及对应部门的记录,包括没有对应部门编号department_id的员工记录。
select e.last_name,e.department_id,d.department_name
from employees e
right outer join departments d
on (e.department_id = d.department_id);
select e.last_name, d.department_id,d.department_name
from employees e, departments d
where e.department_id(+) = d.department_id; 右外连接
结果为:所有员工及对应部门的记录,包括没有任何员工的部门记录。
总结:
A 表right outer join B表
结果是:所有B表记录,及匹配的A表记录,A表未匹配上的显示null
A(+)=B
A 表 left outer join B
A=B(+)
结果是:所有A表记录,及匹配的B表记录,B表未匹配的显示null
经典总结:
有+号的表不全部显示,对面的表全部显示。
口诀:
右外连接+在左
左外连接+在右
有+不全显
无+就全显