[转] Oracle外连接查询
2009-10-28 11:33
316 查看
讲外连接之前,先举例介绍内连接,也就是一般的相等连接。
select * from a, b where a.id = b.id;
对于外连接,oracle中可以使用“(+)”来表示,9i可以使用left/right/full outer join,下面将配合实例一一介绍。
1. left outer join:左外关联
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(+);结果为:所有员工及对应部门的记录,包括没有对应部门编号department_id的员工记录。
2. right outer join:右外关联
select e.last_name, e.department_id, d.department_name
from employees e
right 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;结果为:所有员工及对应部门的记录,包括没有任何员工的部门记录。
3. full outer join:全外关联
select e.last_name, e.department_id, d.department_name
from employees e
full outer join departments d
on (e.department_id = d.department_id);结果为:所有员工及对应部门的记录,包括没有对应部门编号department_id的员工记录和没有任何员工的部门记录。
其实啊 :外连接就是查两张表 左连接就是左边的表全有值,右边表的值可以为空(+)
右连接是左边表值可以为空(+) 右边表的值全有
全连接是左连接和右连接的并集 好像是这个符号(U)
内连接是左连接和右连接的交集 。。。
其余连接方式:
cross join: 交叉连接,查出的表的笛卡尔积 eg: select * from A cross join B (A表有M行,B表有N行,结果是M*N行)。
左连接
a.id=b.id(+) ===> a表内容全部显示,以左边的表为基准
left join : left join 左边的表全部显示,以左边的表为基准;
select * from emp e left join dept d on e.deptno=d.deptno;
右连接 a.id(+)=b.id ===> b表内容全部显示,以右表的表为基准。
right join: right join 右边的表全部显示,以右边的表为基准
select * from emp e right join dept d on e.deptno=d.deptno;
right join ,left join 没有 where from 语句等。
注:+号在=号左边叫右连接,+号在=右边叫做左连接。
自然连接:natural join
自然连接,natural join 会根据列明,自动创建连接,省略where语句,避免笛卡尔积的出现
eg: select empno,ename,sal,deptno,loc from emp natural join dept;
using :表示与指定的列相关联。
eg: select e.ename,e.sal,deptno,d.loc from emp e join dept d using (deptno) where deptno=20;
注:被using 子句所引用的列,在sql 语句中的任何地方不能使用表名或者别名作为前缀。
PS :做外连接的时候,where 条件中不可以加从表的条件,需将从表做个子查询,做成另外一个表。
eg :select t.acctype, nvl(b.name,t.acctype) as name
from biacciccardmaptb t, dictcodesettb b
where b.category = '账户类型'
and t.acctype=b.code(+)
and t.iccardno = '1000751090001385' this is wrong
should :
select t.acctype, nvl(b.name,t.acctype) as name
from biacciccardmaptb t, (select * from dictcodesettb where category = '账户类型' )b
where
t.acctype=b.code(+)
and t.iccardno = '1000751090001385'
原文:http://blog.csdn.net/tanghongru1983/archive/2009/08/10/4431996.aspx
select * from a, b where a.id = b.id;
对于外连接,oracle中可以使用“(+)”来表示,9i可以使用left/right/full outer join,下面将配合实例一一介绍。
1. left outer join:左外关联
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(+);结果为:所有员工及对应部门的记录,包括没有对应部门编号department_id的员工记录。
2. right outer join:右外关联
select e.last_name, e.department_id, d.department_name
from employees e
right 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;结果为:所有员工及对应部门的记录,包括没有任何员工的部门记录。
3. full outer join:全外关联
select e.last_name, e.department_id, d.department_name
from employees e
full outer join departments d
on (e.department_id = d.department_id);结果为:所有员工及对应部门的记录,包括没有对应部门编号department_id的员工记录和没有任何员工的部门记录。
其实啊 :外连接就是查两张表 左连接就是左边的表全有值,右边表的值可以为空(+)
右连接是左边表值可以为空(+) 右边表的值全有
全连接是左连接和右连接的并集 好像是这个符号(U)
内连接是左连接和右连接的交集 。。。
其余连接方式:
cross join: 交叉连接,查出的表的笛卡尔积 eg: select * from A cross join B (A表有M行,B表有N行,结果是M*N行)。
左连接
a.id=b.id(+) ===> a表内容全部显示,以左边的表为基准
left join : left join 左边的表全部显示,以左边的表为基准;
select * from emp e left join dept d on e.deptno=d.deptno;
右连接 a.id(+)=b.id ===> b表内容全部显示,以右表的表为基准。
right join: right join 右边的表全部显示,以右边的表为基准
select * from emp e right join dept d on e.deptno=d.deptno;
right join ,left join 没有 where from 语句等。
注:+号在=号左边叫右连接,+号在=右边叫做左连接。
自然连接:natural join
自然连接,natural join 会根据列明,自动创建连接,省略where语句,避免笛卡尔积的出现
eg: select empno,ename,sal,deptno,loc from emp natural join dept;
using :表示与指定的列相关联。
eg: select e.ename,e.sal,deptno,d.loc from emp e join dept d using (deptno) where deptno=20;
注:被using 子句所引用的列,在sql 语句中的任何地方不能使用表名或者别名作为前缀。
PS :做外连接的时候,where 条件中不可以加从表的条件,需将从表做个子查询,做成另外一个表。
eg :select t.acctype, nvl(b.name,t.acctype) as name
from biacciccardmaptb t, dictcodesettb b
where b.category = '账户类型'
and t.acctype=b.code(+)
and t.iccardno = '1000751090001385' this is wrong
should :
select t.acctype, nvl(b.name,t.acctype) as name
from biacciccardmaptb t, (select * from dictcodesettb where category = '账户类型' )b
where
t.acctype=b.code(+)
and t.iccardno = '1000751090001385'
原文:http://blog.csdn.net/tanghongru1983/archive/2009/08/10/4431996.aspx
相关文章推荐
- C#连接Oracle查询数据乱码问题解决
- Oracle连接查询条件分析
- C#连接Oracle查询更新数据
- 两张图解读Oracle 数据库的 union all、union、intersect、minus以及各种连接查询。
- 转:oracle连接查询
- 查询所有连接到ORACLE服务器的客户端IP地址的方法
- oracle 内连接 外连接 查询 笔记
- Oracle中内连接查询(inner join),自然连接查询(natural join),自连接查询,外连接查询(outer join),左外连接(left join),右外连接(right joi
- oracle dblink 远程连接查询
- Oracle学习笔记 -- day05 多表查询、连接查询、子查询、分页、行转列、集合运算
- oracle进程(连接)数查询
- oracle心得4--集合查询@oracle中的多表连接@案例分析
- VS2010连接Oracle在查询数据时报"指定的转换无效"
- ORACLE 的游标、存储过程、触发器以及表的连接查询
- Oracle学习(5)---数据定义语言---连接查询
- Oracle_学习使用SQL语句六(连接查询)
- oracle 如何查询当前连接的用户名
- Oracle连接查询
- VS2008 IDE连接 ORACLE后新建查询 无响应或者长时间等待
- Oracle的连接查询