oracle中表的左外连接,右外连接,全外连接

oracle中表的左外连接,右外连接,全外连接



create table employees(
last_name varchar2(20),
department_id number(6)
)
/

insert into employees(last_name,department_id)
values('eygle',1);
insert into employees(last_name,department_id)
values('bity_rainy',2);
insert into employees(last_name,department_id)
values('piner',3);
insert into employees(last_name,department_id)
values('coolyl',5);

create table departments(
department_id number(6),
department_name varchar2(20)
)
/
insert into departments(department_id,department_name)
values(1,'alibaba');
insert into departments(department_id,department_name)
values(2,'oracle');
insert into departments(department_id,department_name)
values(3,'ibm');
insert into departments(department_id,department_name)
values(4,'sun');


---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的员工记录和没有任何员工的部门记录。

你可能感兴趣的:(oracle,IBM,sun)