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

Oracle优化查询改写(第三章-操作多个表)

2017-12-03 00:00 387 查看
3.1 UNION ALL 与空字符串(略)

3.2 UNION 与 OR(略)

3.3组合相关的行(略)

3.4 IN,EXISTS和INNER JOIN (略)

3.5 INNER JOIN ,LEFT JOIN 和RIGHT JOIN 和FULL JOIN(略)

drop table L purge;
drop table R purge;
create table L as
select 'left_1' AS str,'1' AS v from dual union all
select 'left_2','2' AS v from dual union all
select 'left_3','3' AS v from dual union all
select 'left_4','4' AS v from dual;

create table R AS
select 'right_3' AS str,'3' AS v,1 AS status from dual union all
select 'right_4' AS str,'4' AS v,0 AS status from dual union all
select 'right_5' AS str,'5' AS v,0 AS status from dual union all
select 'right_6' AS str,'6' AS v,0 AS status from dual

3.6 自关联(略)

3.7NOT IN,NOT EXISTS和LEFT JOIN

有些单位的部门中一个员工也没有,只是设了一个部门的名字。

select count(*) from emp where deptno=40;

COUNT(*)
----------
0

select * from dept where deptno
not in (select emp.deptno from emp where emp.deptno is NOT NULL)

select * from dept where
not exists (select null from emp where emp.deptno = dept.deptno)

select dept.* from dept left join emp on emp.deptno = dept.deptno
where emp.deptno is null;

3.8外连接中的条件不要乱放

select l.str as left_str,r.str as right_str,r.status
from l
left join r on (l.v = r.v and r.status=1)
order by 1,2;

select l.str as left_str,r.str as right_str,r.status
from l
left join (select * from r where r.status =1) r on (l.v = r.v)
order by 1,2;

3.9检测两表中的数据及对应数据条数是否相同

3.10 聚集与内连接

3.11聚集与外连接

3.12从多个表中返回丢失的数据

3.13多表查询时的空值处理

select a.ename,a.comm from emp a
where coalesce(a.comm,0) < (select b.comm from emp b where b.ename='ALLEN')
把NULL值转为0,再比较
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: