in/exists not in/not exists null
in/not in exists/not exists null的理解
两个测试表
create table tmp01 as
with tmp as (
select '1' as id from dual
union all
select '2' as id from dual
union all
select '3' as id from dual
union all
select null as id from dual
)
select * from tmp;
create table tmp02 as
with tmp as (
select '1' as id from dual
union all
select '2' as id from dual
union all
select null as id from dual
)
select * from tmp;
select * from tmp01 t where t.id in (select * from tmp02);
ID
in可以理解为 t.id = 1 or t.id = 2 or t.id = null
select * from tmp01 t where t.id not in (select * from tmp02);
ID
no rows
not in 可以理解为 t.id <> 1 and t.id <> 2 and t.id <> null
由于t.id <> null,为unkown,始终返回false,所以查不出值来。
解决:
select * from tmp01 t where t.id not in (select * from tmp02 where id is not null) or t.id is null;
---------------------------------------------------------------------------------------------------------------------------------
exists实际上用的也是等值判断
select * from tmp01 t where exists (select 'X' from tmp02 d where d.id = t.id);
ID
子查询中,d.id = t.id 判断出来的只有 1 ,2,null = null不可判断,null只能使用 is null,is not null.
select * from tmp01 t where not exists (select 'X' from tmp02 d where d.id = t.id);
ID
此语句查出了null和3,子查询中查出的是1,2,不在这个集合中的有null和3
再说一下 in/exists的效率问题
两个表中一个数据量小,一个数据量大,则子查询表大的用exists,表小的用in
表tmp01(小表),表tmp02(大表)
select * from tmp01 where id in (select id from tmp02)
效率低,用到了tmp01 表上id 列的索引
select * from tmp01 where exists(select id from tmp02 where id= tmp01.id)
效率高,用到了tmp02 表上id 列的索引。
select * from tmp02 where exists(select id from tmp01 where id= tmp02.cc)
效率高,使用tmp02 表上id 列的索引
select * from tmp02 where exists(select id from tmp01 where id= tmp02.cc)
效率低,用到了tmp01表上id列的索引
not in/not exists
not in内外表进行全表扫描,不使用索引
not extsts 的子查询用到表上的索引。无论表大小,用not exists 都比not in 要快。
in/exists not in/not exists null的更多相关文章
- SQL Server-聚焦NOT IN VS NOT EXISTS VS LEFT JOIN...IS NULL性能分析(十八)
前言 本节我们来综合比较NOT IN VS NOT EXISTS VS LEFT JOIN...IS NULL的性能,简短的内容,深入的理解,Always to review the basics. ...
- magento -- 解决magento错误:ERROR: Base table or view already exists: 1050 Table ... already exists
相信有更新magento或者,备份转移magento站点的时候可能会碰到类似这样的错误提示: Base table or view already exists: 1050 Table ... alr ...
- oracle中的exists 和not exists 用法 in与exists语句的效率问题
博文来源(oracle中的exists 和not exists 用法):http://chenshuai365-163-com.iteye.com/blog/1003247 博文来源( in与exi ...
- sql server if exists和 if not exists 的关键字用法
if exists和if not exists关键字用法 1.介绍 if not exists 即如果不存在,if exists 即如果存在 2.使用 a.判断数据库不存在时 if not ...
- oralce中exists not exists in not in对于NULL的处理
1. 先讨论 in 与 not in中存在NULL的情况, sql语句如下: 1 select 1 result1 from dual where 1 not in (2, 3); 2 3 4 s ...
- not exists、left join/is null、not in 行为
测试数据 20:25:52[test](;)> select * from t;+------+------+| id | b |+------+------+| 1 | NUL ...
- sqlserver exists和in 与exists和not in
1.exists 和 in 1.1 正常情况下exists和in的效果是一样的,如图试验 即使子查询中包含null也没有关系,依然可以正常使用 1.2 in 和 exists效率比较 先看in 由图中 ...
- if exists和if not exists关键字用法
在sql语名中,if not exists 即如果不存在,if exists 即如果存在. 下面学习下二者的用法. a,判断数据库不存在时 代码示例: if not exists(select * f ...
- SQL Server-聚焦LEFT JOIN...IS NULL AND NOT EXISTS性能分析(十七)
前言 本节我们来分析LEFT JOIN和NOT EXISTS,简短的内容,深入的理解,Always to review the basics. LEFT JOIN...IS NULL和NOT EXIS ...
随机推荐
- Mysql权限控制 - 允许用户远程连接
Mysql为了安全性,在默认情况下用户只允许在本地登录,可是在有此情况下,还是需要使用用户进行远程连接,因此为了使其可以远程需要进行如下操作: 一.允许root用户在任何地方进行远程登录,并具有所有库 ...
- CCScrollView/CCTableView(CCTableViewDelegate CCTableViewDataSource CCTableView-滑动列表-游戏中大量使用 非常重要的一个类)
tableview scrollViewDidScroll函数中有一段 ---- 即---滑动tableview时触发的函数 : 会将全部显示的cell又一次刷新(刷新函数中调用了自己定义的ta ...
- 微信公共服务平台开发(.Net 的实现)10-------地理位置
微信公共平台中涉及到地理位置的有两种情况: 第一.我发送一个自选的地理位置给微信,然后微信可以自动反馈响应的信息. 第二.让微信获取我们GPS定位地址位置,反馈响应的信息. 首 ...
- MySql之char与varchar
MySql之char与varchar的差别 char是一种固定长度的类型,varchar则是一种可变长度的类型.它们的差别是: 1. char(M)类型的数据列里.每一个值都占用M个字节.假设某个长 ...
- javascript 基本数据类型
1. javascript 五种基本数据类型 Undefined Boolean Number String Null,Undefined 对应的值只有一个 undefined, Boolean 对 ...
- listen和accept函数
listen函数是用来设置监听连接的句柄和队列 当listen函数执行完成以后,服务端就已经可以接受客户端来的新连接了,新连接完成以后listen会把客户端的ip,port和连接句柄放在监听队列里面, ...
- careercup-高等难度 18.1
18.1 编写一个函数,将两个数字相加,不得使用+或其他算术运算符. int add(int a,int b) { ) return a; int sum=a^b; ; return add(sum ...
- 在asp.net mvc中将checkbox传到后台时总是true的解决方法
我今天在做同城交友网站(www.niyeuwo.com)时发现,不管checkbox是否选 中,传到Controller时总是true,后来在查网上查了资料才知道,原来是jQuery在传值时写错了. ...
- WIN7 下 Qt Creator 安装 QWT
WIN7 下 Qt Creator 安装 QWT 环境:WIN7 +QT Creator2.6.2 1.下载QWT源代码 qwt-6.1-rc3.zip 2 编译QWT open projects- ...
- 在应用程序中实现对NandFlash的操作
以TC58NVG2S3ETA00 为例: 下面是它的一些物理参数: 图一 图二 图三 图四 图五 图6-0 图6-1 说明一下,在图6-1中中间的那个布局表可以看做是实际的NandFlash一页数据的 ...