本文主要介绍了mysql exists与not exists实例详解的相关资料,鉴于 not exists 的效率往往要高于 not in , 所以一般情况下会使用前者替代后者,需要的朋友可以参考下,希望能帮助到大家。
mysql exists与not exists实例详解
tablea
|column1 | column1 |column3 |
tableb
|column1 | column1 |column3 |
要查询 tablea 的数据,条件是是 tablea.column1 不在 tableb 的 tableb.column2 中
也就是要得到类似以下语句的效果(not in 效果不完全等同于 not exists , 如果子查询中出现空记录, 则整个查询语句不会返回数据)
select
a.*
from
tablea a
where
a.column1 not in (
select column2 from tableb
)
可以使用如下语句来实现
select
a.*
from
tablea a
where
not exists(
select b.column2 from tableb b where a.colunm1=b.column2
)
以上只是两张表的情况, 其实在多张表的连接查询中也是比较好用的. 以上写法同样适用于exists
相关推荐:
php file_exists()函数没有效果是因为什么?
有关exists()的文章推荐10篇
有关php file_exists()函数的文章推荐10篇
以上就是关于mysql exists与not exists分析的详细内容。
