Oracle SQL Lesson (6) - 使用Join进行联合查询

时间:2023-11-12 11:55:08

使用连接
SQL 1999
SELECT table1.column, table2.column
FROM table1
[NATURAL JOIN table2] |
[JOIN table2 USING (column_name)] |
[JOIN table2
ON (table1.column_name = table2.column_name)]|
[LEFT|RIGHT|FULL OUTER JOIN table2
ON (table1.column_name = table2.column_name)]|
[CROSS JOIN table2];

自然连接:
SELECT department_id, department_name, location_id, city
FROM departments NATURAL JOIN locations ;

select department_id, department_name,department.location_id, city
from departments, locations
where departments.location_id = locations.location_id

create table t1(c1 number, c2 number, c3 number, c4 number);
create table t2(c1 number, c2 number, c3 number);
insert into t1 values(1,1,1,1);
insert into t1 values(2,2,11,11);
insert into t1 values(3,3,3,4);
insert into t2 values(1,11,11);
insert into t2 values(1,11,11);
insert into t2 values(2,2,111);
insert into t2 values(2,2,111);
insert into t2 values(3,3,3);
insert into t2 values(3,3,3);
select * from t1 natural join t2;
select t1.c1,t1.c2,t1.c3 from t1,t2
where t1.c1 = t2.c1
and t1.c2 = t2.c2
and t1.c3 = t2.c3

using子句
select * from t1 join t2 using(c1);
select * from t1 join t2 using(c1,c2);
oracle语法
select t1.c1,t1.c2,t1.c3 from t1,t2 where t1.c1 = t2.c1
select t1.c1,t1.c2,t1.c3 from t1,t2 where t1.c1 = t2.c1 and t1.c2 = t2.c2;

SELECT employee_id, last_name,
location_id, department_id
FROM employees JOIN departments
USING (department_id) ;

using列出现在别的位置,不能用别名限定
SELECT l.city, d.department_name
FROM locations l JOIN departments d
USING (location_id)
WHERE d.location_id = 1400; (Not OK)

SELECT l.city, d.department_name
FROM locations l JOIN departments d
USING (location_id)
WHERE location_id = 1400; (OK)

on子句
SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM employees e JOIN departments d
ON (e.department_id = d.department_id);

SELECT e.employee_id, e.last_name, department_id, d.location_id
FROM employees e JOIN departments d
USING (department_id);

SELECT employee_id, city, department_name
FROM employees e
JOIN departments d
ON d.department_id = e.department_id
JOIN locations l
ON d.location_id = l.location_id;

oracle语法
SELECT employee_id, city, department_name
FROM employees e,departments d, locations l
WHERE d.department_id = e.department_id
AND d.location_id = l.location_id;

在join中应用额外条件
SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM employees e JOIN departments d
ON (e.department_id = d.department_id)
AND e.manager_id = 149 ;

SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM employees e JOIN departments d
ON (e.department_id = d.department_id)
WHERE e.manager_id = 149 ;

自连接:
SELECT worker.last_name emp, manager.last_name mgr
FROM employees worker JOIN employees manager
ON (worker.manager_id = manager.employee_id);

非等值连接
select ename, sal, grade
from emp join salgrade
on (sal between losal and hisal);

select ename, sal, grade
from emp,salgrade
where sal between losal and hisal;

外连接
create table e as select * from emp;
create table d as select * from dept;
select distinct deptno from e;
select deptno from d;
select ename, e.deptno,d.deptno from e join d on (e.deptno = d.deptno);
select ename, e.deptno,d.deptno from e right join d on (e.deptno = d.deptno);
select ename, e.deptno,d.deptno from e,d where e.deptno(+) = d.deptno;

insert into e values(8888,'ZhangHua','salesman',7698,'08-SEP-87',800,0, 50);
select ename, e.deptno,d.deptno from e left join d on (e.deptno = d.deptno);
select ename, e.deptno,d.deptno from e,d where e.deptno = d.deptno(+);

交叉连接(笛卡尔积)
SELECT ename,dname FROM emp cross join dept;
SELECT ename,dname FROM emp,dept;