现在的位置: 首页 > 综合 > 正文

Oracle(二)-多表链接查询

2013年11月06日 ⁄ 综合 ⁄ 共 2980字 ⁄ 字号 评论关闭

多表关联查询
如果未指定连接条件,则结果返回是个笛卡尔乘积
比如:
select employee_id, department_id, location_id from employees, departments;
大多数情况下,笛卡尔乘积不是我们想要的结果,我们一般要在Where子句中提供链接条件,对于链接,通常又包括多种类型:
不同的数据库厂商对链接类型有不同的定义,国际上有个凌驾于各个厂商的工业标准定义(SQL1999)。

Oracle定义的链接类型:
1、等于链接
语法:select table1.column, table2.column from table1, table2 where table1.column1 = table2.column2;
举例:select employees.employee_id, employees.last_name, employees.department_id,
departments.department_id, departments.location_id
FROM employees, departments
WHERE employees.department_id = departments.department_id;
2、不等链接
语法:select table1.column, table2.column from table1, table2 where table1.column1 > table2.column2;
举例:select e.last_name, e.salary, j.grade_level from employees e, job_grades j where e.salary
between j.lowest_sal and j.highest_sal;
3、外链接(左外链接、右外链接)
语法:(左外链接)select table1.column, table2.column from table1, table2 where table1.column(+) = table2.column;
(右外链接)select table1.column, table2.column from table1, table2 where table1.column = table2.column(+)
小规律:没有括号的外联有括号的,即当没有共同的column的时候,没有括号的数据依然会显示出来。
举例:select e.last_name, e.department_id, d.department_name from employees e, departments d
where e.department_id(+) = d.department_id;
4、自链接(即某个table和自己本身链接)
语法:select table1.column, table2.column from table1, table1 table2 where table1.column1 = table2.column2
举例:选择某个员工和经理的从属关系
select worker.last_name || ‘ works for ‘ || manager.last_name
from employees worker, employees manager
where worker.manager_id = manager.employee_id;

SQL1999定义的链接类型(Oracle从9i开始提供对SQL1999的兼容支持):
SQL1999的语法如下(一句SQL代码概括):
select table1.column, table2.column
from table1
[cross join table2] |
[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)];

1、交叉链接
相当于没有链接条件的多表关联查询,结果相当于笛卡尔乘积,实际工作中很少用到
举例:select last_name, department_name from employees cross join departments;
2、自然链接
相当于Oracle的“等于链接”,只不过是让系统自己去找两张表中字段名相同的字段作为“等于链接”的条件;
(PS:如果两个表中有相同的列名,但字段类型不一样,就会引发错误)
举例:select department_id, department_name, location_id, city
from departments
natural join locations;
相当于:select department_id, department_name, location_id, city
from departments, locations
where departments.location_id = locations.location_id;
3、Using子句
算是自然链接的一种补充功能,自然链接会让系统自动查找两张表中所有列名相同的字段,并试图建立”等于链接“;
但有的时候我们不期望这么做,而是只期望某个特定的字段作为”等于链接“的条件,这种情况下可以使用Using子句来做限制。
举例:想建立链接,又想控制只使用department_id作为链接条件
select e.employee_id, e.last_name, d.location_id
from employee e join departments d
using (department_id);
4、内链接
相当于Oracle的”等于链接“,关键字:inner join,可省略inner
举例:select employee_id, city, department_name
from employees e
inner join departments d on d.department_id = e.department_id
inner join locations l on d.location_id = l.location_id;
5、外链接(全外链接、左外链接、右外链接)
举例:
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, e.department_id, d.department_name
from employees e, departments d
where e.department_id(+)=d.department_id
union
select e.last_name, e.department_id, d.department_name
from employees e, departments d
where e.department_id=d.department_id(+);
上个例子是左外链接的例子,右外连接、全外链接同理。

附件下载:

多表链接查询

抱歉!评论已关闭.