SQL语句中的多表链接

网络整理 - 07-27

  ---1. 普通的相等连接

  select e.last_name, d.department_name

  from employees e, departments d

  where e.department_id = d.department_id;

  ---2 左外连接 LEFT OUTER JOIN

  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的员工记录。

  ----3. 右外连接 right outer join

  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;

  结果为:所有员工及对应部门的记录,包括没有任何员工的部门记录。

  ----4.FULL OUTER JOIN:全外关联

  select e.last_name, e.department_id, d.department_name

  from employees e

  full outer join departments d

  on (e.department_id = d.department_id);

  结果为:所有员工及对应部门的记录,包括没有对应部门编号department_id的员工记录和没有任何员工的部门记录。