您的位置:首页 > 数据库 > Oracle

Oracle多表查询

2016-04-25 15:37 525 查看
多表查询数据可以是两个表或者视图,也可以是两个以上的表或者视图;

1.从多表查询数据也被称为多表连接

2、连接查询的目的是为用户提供更详细的数据

3、有各种各样的连接方式;

----

如:select e.employee_id,e.last_Name,d.department_id,d.department_name from emp e,dep d where e.department_id=d.department_id;

oracle SQL 的连接类型有下面几种:equjion 等值连接,Non-equjion非等值连接,outer join外连接,self join 自连接,外连接分为:左外连接,右外连接,全外连接;

为了连接n个表在一起,你最少需要n-1个连接条件,例如:为了连接4个表,最少需要3个连接条件。如果表中有一个连接主键,该规则可能不适用,其中可能有多行用来唯一地标识每一行。

等值连接:

等值连接是使用最多的一种连接方式;连接条件是两个表的某些列相等;等值连接就是将两个子表重新组全为一个大表

如:

select emp.employee_id,emp.last_name,emp.department_id,dep.department_id,dep.location_id from emp,dep where emp.department_id = dep.department_id;

或:

select employee_id,last_name,emp.department_id,dep.department_id,location_id from emp,dep where emp.department_id=dep.department_id;

如:

select employee_id,last_name,emp.department_id,dep.department_id,location_id from emp,dep where emp.department_id=dep.department_id and last_name like 'a%';

连接中的列别名、表别名;

如果连接的两个(或两个以上)表中有名字相同的列,那么此列的形式是'表名,列名’ 对于同名列表中不可以省略

非等值连接

连接条件不是两个表的某列(或某些列)相等,就是非等值连接 非等值连接使用并不是很多

如:

select e.last_name,e.salary,j.job_title from Emp e,jobs j where e.salary between j.min_salary and j.max_salary;

或:

select e.last_name,e.salary,j.job_title from emp e,jobs j where e.salary>=j.min_salary and e.salary <=j.max.salary;

外连接:

普通的连接被称为内连接。

内连接的特点是如果一个行不满足连接条件,该行将不出现在查询结果中。

使用外连接,不满足连接条件的行也会在输出结果中。

如:

select table1.column,table2.column from table1,table2 where table1.column(+)=table2.column;

显示不满足条件的行;

select table1.column,table2.column from table1,table2 where table1.cloumn=table2.column(+);

无法进行这样的连接:

select table1.column,table2.column from table1,table2 where table1.column(+)=table2.column(+);

全外连接如下:

select table1.column,table2.column from table1 full outer join table2 on(table.column=table2.column);

左外连接:

select e.last_name,e.department_id,d.department_name from emp e left outer join dep d on (e.department_id=d.department_id);

右外连接:

select e.last_name,d.department_id,d.department_name from emp e right out join dep d on(e.department_id = d.department_id);

全外连接:

select e.last_name,e.department_id,d.department_name from emp e full outer join dep d on(e.department_id = d.department_id);

自连接:

自连接就是连接的两个表均来自于同一个列,使用很少,是等值或非等值的连接 。

如:

select w.last_name|| 'work for ' || m.last_name from emp w,emp m where w.manager_id = m.emp_id;

自连接示例:

select w.last_name|| 'works for' || m.last_name from emp w,emp m where w.manager_id=m.employee_id;

Oracle中SQL99的连接语法;

交叉连接:

select last_name,department_name from emp cross join department;

等价于笛卡儿积;

自然连接:

select department_id,departme_name,location_id,city from departments natural join locations;

等价于等值连接,前提是两个表有相同名称的列。如果没有,则为交叉连接;

如果列名称相同,但是列类型不同,会报错。可以使用using子句来处理这种情况。自然连接用具有相匹配的名字和数据类型的所有列来连接表。using子句可以被用来指定那些被用语一个等值连接的列中的唯一列。在using子句中引用的例不应该在sql语句的任何地方用表名或表别名限制。

如:select l.city,d.department_name from locations l join departments d using(location_id) where location_id=1400;

select e.employee_id,e.last_name,d.location_id from employees e join departments d using(department_id);

on 子句:

对于自然连接的连接条件,基本上是带有相同名字的所有列的等值连接,为了指定任意条件,或者指定要连接的列,可以使用On子句连接条件从另一个搜索条件中被分开,On子句使得代码易懂。

select e.employee_id,e.last_name,e.department_id,d.department_id,d.location_id from emp e join dep d on(e.department_id=d.department_id);

On 子句等价于等值连接和不等值连接。

用On子句创建三向连接;

select employee_id,city,department_name from emp e join dep d on d.department_id=e.department_id join locations l on d.location_id =l.location_id;

总结:

有多种方法可以进行表的连接 ,等值Equijoins.非等值Non_equijoins,外连接outer joins,自连接 Self joins,交叉连接 Cross joins.自然连接 Natural joins.全外连接 full or outer joins.最好不要产生笛卡儿积,性能会变差。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: