內連線和外連線的區別
阿新 • • 發佈:2019-01-25
drop table employees;
drop table departments;
--建立資料表
--父表
create table departments(
department_id int(10),
department_name varchar(15)
);
--子表
create table employees(
employee_id int(3),
employee_name varchar(15),
salary int(15),
department_id int(10)
);
insert into departments(department_id,department_name) values(10,'開發部');
insert into departments(department_id,department_name) values(20,'測試部');
insert into departments(department_id,department_name) values(30,'財務部');
insert into departments(department_id,department_name) values(40,'人力資源部');
insert into employees(employee_id,employee_name,salary,department_id) values(1,'張三',10000,'10');
insert into employees(employee_id,employee_name,salary,department_id) values(2,'李四',10000,'20');
insert into employees(employee_id,employee_name,salary,department_id) values(3,'王五',10000,'30');
insert into employees(employee_id,employee_name,salary,department_id) values(4,'陳偉',10000,null);
commit;
--第一種寫法
--內連線
select e.employee_id,e.employee_name,e.salary,d.department_name
from employees e , departments d
where e.department_id = d.department_id;
--左外連線
select e.employee_id,e.employee_name,e.salary,d.department_name
from employees e , departments d
where e.department_id = d.department_id(+);
--右外連線
select e.employee_id,e.employee_name,e.salary,d.department_name
from employees e , departments d
where e.department_id(+) = d.department_id;
--第二種寫法(sql99標準增加的)
--內連線
select e.employee_id,e.employee_name,e.salary,d.department_name
from employees e join departments d
on (e.department_id = d.department_id);
--左外連線
select e.employee_id,e.employee_name,e.salary,d.department_name
from employees e left outer join departments d
on (e.department_id = d.department_id);
--右外連線
select e.employee_id,e.employee_name,e.salary,d.department_name
from employees e right outer join departments d
on (e.department_id = d.department_id);
drop table departments;
--建立資料表
--父表
create table departments(
department_id int(10),
department_name varchar(15)
);
--子表
create table employees(
employee_id int(3),
employee_name varchar(15),
salary int(15),
department_id int(10)
);
insert into departments(department_id,department_name) values(10,'開發部');
insert into departments(department_id,department_name) values(20,'測試部');
insert into departments(department_id,department_name) values(30,'財務部');
insert into departments(department_id,department_name) values(40,'人力資源部');
insert into employees(employee_id,employee_name,salary,department_id) values(1,'張三',10000,'10');
insert into employees(employee_id,employee_name,salary,department_id) values(2,'李四',10000,'20');
insert into employees(employee_id,employee_name,salary,department_id) values(3,'王五',10000,'30');
insert into employees(employee_id,employee_name,salary,department_id) values(4,'陳偉',10000,null);
commit;
--第一種寫法
--內連線
select e.employee_id,e.employee_name,e.salary,d.department_name
from employees e , departments d
where e.department_id = d.department_id;
--左外連線
select e.employee_id,e.employee_name,e.salary,d.department_name
from employees e , departments d
where e.department_id = d.department_id(+);
--右外連線
select e.employee_id,e.employee_name,e.salary,d.department_name
from employees e , departments d
where e.department_id(+) = d.department_id;
--第二種寫法(sql99標準增加的)
--內連線
select e.employee_id,e.employee_name,e.salary,d.department_name
from employees e join departments d
on (e.department_id = d.department_id);
--左外連線
select e.employee_id,e.employee_name,e.salary,d.department_name
from employees e left outer join departments d
on (e.department_id = d.department_id);
--右外連線
select e.employee_id,e.employee_name,e.salary,d.department_name
from employees e right outer join departments d
on (e.department_id = d.department_id);