Home  >  Article  >  Database  >  Oracle数据库之SQL连接查询

Oracle数据库之SQL连接查询

WBOY
WBOYOriginal
2016-06-07 17:28:001015browse

现实中有这么一种需求,有时候我们需要查询的数据是在多个表中的,那么如何从多个表中查询数据呢?这就需要通过表的连接来实现查询

前言

现实中有这么一种需求,有时候我们需要查询的数据是在多个表中的,那么如何从多个表中查询数据呢?这就需要通过表的连接来实现查询了。

笛卡尔积

在定义连接之前我们需要简单的了解一下笛卡尔积,笔者不会纠结与其定义,只是看看其实际效果

其实笛卡尔积就是没有连接条件或者连接条件无效的连接,例如:

select *from emp ,dept;--其结果倾向于一个巨大的记录数,无实际意义

上面可以看出笛卡尔积得出的是一个巨大的无意义的记录集合,我们可以通过在where子句中使用有效的连接来避免这种情况,使之具有实际意义。

连接定义

就是在笛卡尔积的基础上使用连接条件,例如根据两个表的相同列进行连接。若果要进行n个表的连接那么就的添加n-1个连接条件。例如:

select *from emp ,dept where emp.deptno = dept.deptno;

连接类型

连接主要有两种类型等值连接、非等值连接.

还包括一些其他的连接方式:多连接、自连接、 定置运算符

等值连接

上面已经使用过了,即是表连接的where条件是一个表的列等于另一个表的列,通常情况下是主键和外键的判等连接。

select *from emp ,dept where emp.deptno = dept.deptno;

Tips:因为deptno在两个表中都存在,所以要使用表.字段的形式,否则Oracle认为其歧义

等值连接中同样还可以添加约束条件

select *from emp ,dept where emp.deptno = dept.deptno and emp.ename ='CLARK';

--结果只取一条记录

同样可以定义标的别名,不过需要注意的是定义了别名之后,,必须通过别名访问字段,不能通过表明来访问了

select *from emp e ,dept d where e.deptno = d.deptno and e.ename ='CLARK';

非等值连接

通过观察scott用户下的emp表和salgrade表,可以知道的是他们之间没有直接的对应项,而是

emp的sal列的值在salgrade表中的LOSAL和HISAL之间,他们不是等值连接。

select * from emp e,salgrade s where e.sal between s.losal and s.hisal;

linux

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn