用Not Exists 代替Not In
Not Exists允许用户使用相关子查询已排除一个表中能够与另一个表成功连接的所有记录。
Select a.mobileid
from Log_user a
where not exists
(select b.mobileid from magazineitem b
where b.mobileid=a.mobileid);
对于外查询的每条记录(Log_user),not exists字查询都要测试。如果这条记录于Magazineitem连接后返回一行,则子查询成功。Not Exists 通知查询对返回的代码取反,这样,Log_user 表中与Log_User连接成功的行都不为外查询返回。剩下的行就是那些在magazineitem中没有记录的。
Not Exists用到了连接,能够发挥已经建好的索引的作用,而Not In不能使用索引。
Not In是最慢的方式,要同每条记录比较,在数据量比较大的查询中不建议使用这种方式。
关于Exists和IN:
Select name,skill
from workerstill ws
where exists
(select * from workskill
where ws.name=name
group by name
having count(skill)>1);
检查每个外查询的值,ExistS都要测试,如果为真,那么外查询选择一个姓名和技能
EXISTS是存在性的一种测试,特点:
1。不能匹配一列或者多列
2。只能用于相关的子查询
转载于:https://www.cnblogs.com/cxd4321/archive/2007/05/29/764150.html
用Not Exists 代替Not In相关推荐
- MySQL 学习笔记(16)— 子查询(单行单列、一行多列、多行多列、 ALL、ANY、SOME 运算符、EXISTS 操作符)
1. 子查询概念 子查询是指嵌套在其他语句(SELECT . INSERT . UPDATE . DELETE 等)中的 SELECT 语句:子查询也称为内查询( inner query )或者嵌套查 ...
- oracle中的exists 和 not exists 用法详解
from:http://blog.sina.com.cn/s/blog_601d1ce30100cyrb.html oracle中的exists 和 not exists 用法详解 (2009-05- ...
- SQL:EXISTS的用法理解(转)
摘自:http://www.cnblogs.com/netserver/archive/2008/12/25/1362615.html 比如在Northwind数据库中有一个查询为 SELECT c. ...
- oracle中的exists 和not exists 用法详解
有两个简单例子,以说明 "exists"和"in"的效率问题 1) select * from T1 where exists(select 1 from T2 ...
- SQL with NUll处理,Join系列,between,in对比exists以及少量题目
2019独角兽企业重金招聘Python工程师标准>>> 1.一些题目: 选择在每一组B值相同的数据中对应的a最大的记录的所有信息,(用于论坛每月排行榜) Selecta,b,c fr ...
- Sql 语句中 IN 和 EXISTS
原文链接: (2条消息)Sql 语句中 IN 和 EXISTS 的区别及应用 - jcpp9527的博客 - CSDN博客 https://blog.csdn.net/wqc19920906/art ...
- git:Git fetch和git pull的区别, 解决Git报错:error: You have not concluded your merge (MERGE_HEAD exists)....
Git fetch和git pull的区别, 解决Git报错:error: You have not concluded your merge (MERGE_HEAD exists). 解决办法一:保 ...
- oracle exists mysql_oracle_in_exists_left-join
查询A表中的数据没有出现在B表中 mysql> select * from user; +------+-------+ | uid | uname | +------+-------+ | ...
- oracle exists语句
前言 书上这么写的,先看from,再看where,最后选出满足的行 select * from A where exists (select * from A where Code=1111) 等同于 ...
- sql中exists,not exists的用法
exists : 强调的是是否返回结果集,不要求知道返回什么, 比如: select name from student where sex = 'm' and mark exists(selec ...
最新文章
- Ubuntu18.04 编译 ncnn
- Spring Cloud 随笔:记录在使用 OAuth2 遇到的巨坑
- linux内核tor03,Linux内核x86架构引导协议4(翻译)
- 007_CSS ID选择器
- deepin 15.11镜像下载地址
- ORACLE8的分区管理
- 【TensorFlow】tf.nn.softmax_cross_entropy_with_logits中的“logits”到底是个什么意思?
- 什么是URL转发和一个IP建多个Web站点--主机头名法
- 使用标准库函数对象的例子
- 郁闷的C小加(一)(后缀表达式)
- Autolayout代码实现举例-02-使用VFL
- 利用python画一些简单图像
- 用html和CSS做个人简历
- Backtrack 4 – Bootable USB Thumb Drive with “Full” Disk Encryption
- MISRA C_2012规则翻译、解读、示例
- Python数据分析师工资怎么样?
- 北大计算机前辈徐,九年中获得国家最高科学技术奖的八位北大人
- C语言编程>第十六周 ② 函数fun的功能是:统计长整数test的各位上出现数字5、6、7的次数,并通过外部(全局)变量sum5、sum6、sum7返回主函数。
- [分享]深思--求索人的心智 就是求索长青基业
- 交通 流量 时间序列预测,神经网络 机器学习 BPNN