服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - mysql中EXISTS和IN的使用方法比较

mysql中EXISTS和IN的使用方法比较

2021-04-23 17:12槐月十九 Mysql

这篇文章主要给大家介绍了关于mysql中EXISTS和IN使用方法比较的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1、使用方式:

(1)exists用法

?
1
select a.batchname,a.projectid from ucsc_project_batch a where exists (select b.id from ucsc_project b where a.projectid = b.id)

mysql中EXISTS和IN的使用方法比较

上面这条sql的意思就是:以ucsc_project_batch为主表查询batchname与projectid字段,其中projectid字段存在于ucsc_project表中。

exists 会对外表ucsc_project_batch进行循环查询匹配,它不在乎后面的内表子查询的返回值是什么,只在乎有没有存在返回值,存在返回值,则条件为真,该条数据匹配成功,加入查询结果集中;如果没有返回值,条件为假,丢弃该条数据。

例如我们这里改变一下子查询的查询返回字段,并不影响外查询的查询结果:

?
1
select a.batchname,a.projectid from ucsc_project_batch a where exists (select b.companyid,b.name from ucsc_project b where a.projectid = b.id)

mysql中EXISTS和IN的使用方法比较

(2)in用法

?
1
select a.batchname,a.projectid from ucsc_project_batch a where a.projectid in (select b.id from ucsc_project b)

mysql中EXISTS和IN的使用方法比较

上面这条sql的查询结果与刚才的exists的结果一样,查询的意思也一样。

2、注意点:

(1)exists写法需要注意子查询中的条件语句一般需要带上外查询的表做关联,不然子查询的条件可能会一直为真,或者一直为假,外查询的表进行循环匹配的时候,要么全部都查询出来,要么一条也没有。

?
1
select a.batchname,a.projectid from ucsc_project_batch a where exists (select b.id from ucsc_project b)

比如上述这种写法,由于ucsc_project 表存在值,子查询的条件一直为真,ucsc_project_batch 每条数据进行循环匹配的时候,都能匹配成功,查询出来的结果就成为了ucsc_project_batch整张表数据。

mysql中EXISTS和IN的使用方法比较

?
1
select a.batchname,a.projectid from ucsc_project_batch a where exists (select b.id from ucsc_project b where b.id is null)

这种写法,子查询肯定查不到结果,所以子查询的条件为假,外查询的每条数据匹配失败,整个查询结果为空

mysql中EXISTS和IN的使用方法比较

(2)in语句在mysql中没有参数个数的限制,但是mysql中sql语句有长度大小限制,整段最大为4m

(3)exists的子查询语句不在乎查询的是什么,只在乎有没有结果集存在,存在则整个子查询可以看作一个条件为真的语句,不然就是一个条件为假的语句

(4)in语句对于子查询的返回字段只能由一个,不然会报错:

select a.batchname,a.projectid from ucsc_project_batch a where a.projectid in  (select b.id,b.companyid from ucsc_project b)

[err] 1241 - operand should contain 1 column(s)

3、场景选择

外查询表大,子查询表小,选择in;外查询表小,子查询表大,选择exists;若两表差不多大,则差不多。

(1)in中的sql查询只会查询一次,然后把结果集存在临时文件中,然后再与外层查询sql进行匹配,其中外查询与子查询都可以使用索引

?
1
select a.batchname,a.projectid from ucsc_project_batch a where a.projectid in (select b.id from ucsc_project b)

mysql中EXISTS和IN的使用方法比较

等价于:

?
1
2
3
4
5
6
7
8
9
10
11
$result = [];
$ucsc_project_batch = "select a.batchname,a.projectid from ucsc_project_batch a";
$ucsc_project = "select b.id from ucsc_project b";
for($i = 0;$i < $ucsc_project_batch .length;$i++){
 for($j = 0;$j < $ucsc_project .length;$j++){
  if($ucsc_project_batch [$i].projectid== $ucsc_project [$j].id){
   $result[] = $ucsc_project_batch [$i];
   break;
  }
 }
}

(2)exists会对外查询的表ucsc_project_batch 进行循环匹配,执行ucsc_project_batch.length次,其中子查询可以使用索引,外查询全表扫描

?
1
select a.batchname,a.projectid from ucsc_project_batch a where exists (select b.id from ucsc_project b where a.projectid = b.id)

mysql中EXISTS和IN的使用方法比较

等价于:

?
1
2
3
4
5
6
7
$result = [];
$ucsc_project_batch = "select a.batchname,a.projectid from ucsc_project_batch a ";
for ($i = 0; $i < $ucsc_project_batch . length; $i++) {
 if (exists($ucsc_project_batch [$i] . projectid)) {//执行select b.id from ucsc_project b where a.projectid=b.id       
  $result[] = $ucsc_project_batch [$i];
 }
}

通过两个的伪代码分析可知:子查询的表大的时候,使用exists可以有效减少总的循环次数来提升速度;当外查询的表大的时候,使用in可以有效减少对外查询表循环遍历来提升速度。

总结

到此这篇关于mysql中exists和in的使用方法比较的文章就介绍到这了,更多相关mysql exists和in比较内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/zhenwei1994/article/details/82145711

延伸 · 阅读

精彩推荐