【1】exists

对外表用loop逐条查询,每次查询都会查看exists的条件语句。

当 exists里的条件语句能够返回记录行时(无论记录行是多少,只要能返回),条件就为真 , 返回当前loop到的这条记录。反之如果exists里的条件语句不能返回记录行,条件为假,则当前loop到的这条记录被丢弃。

exists的条件就像一个boolean条件,当能返回结果集则为1,不能返回结果集则为 0。

语法格式如下:

select * from tables_name where [not] exists(select..);
  • 1

示例如下:

select * from p_user_2 where  EXISTS(select * from p_user where id=12)
  • 1

如果p_user表中有id为12的记录,那么将返回所有p_user_2表中的记录;否则,返回记录为空。

如果是not exists,则与上述相反。

总的来说,如果A表有n条记录,那么exists查询就是将这n条记录逐条取出,然后判断n遍exists条件


【2】in

语法格式如下:

select * from A where column in (select column from B);
  • 1

需要说明的是,where中,column为A的某一列,in 所对应的子查询语句返回为一列多行结果集。

注意,in所对应的select语句返回的结果一定是一列!可以为多行。

示例如下:

select * from p_user_2 where id [not] in (select id from p_user )
  • 1

查询id在p_user表id集合的p_user_2的记录。not in则相反。


【3】exists与in的关系

经过sql改变,二者是可以达到同一个目标的:

select * from p_user_2 where id [not] in (select id from p_user );

select * from p_user_2 where [not] EXISTS (select id from p_user where id = p_user_2.id )
  • 1
  • 2
  • 3

那么什么时候用exists 或者in呢?

如果查询的两个表大小相当,那么用in和exists差别不大。 
如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in:

例如:表A(小表),表B(大表)

① 子查询表为表B:

select * from A where cc in (select cc from B) 效率低,用到了A表上cc列的索引;

select * from A where exists(select cc from B where cc=A.cc) 效率高,用到了B表上cc列的索引。
相反的
  • 1
  • 2
  • 3
  • 4

② 子查询表为表A:

select * from B where cc in (select cc from A) 效率高,用到了B表上cc列的索引;

select * from B where exists(select cc from A where cc=B.cc) 效率低,用到了A表上cc列的索引。
  • 1
  • 2
  • 3

not in 和not exists如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引;而not extsts 的子查询依然能用到表上的索引。

所以无论那个表大,用not exists都比not in要快。

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/J080624/article/details/72910548

MySQL - exists与in的用法的更多相关文章

  1. MySQL基础之STRAIGHT JOIN用法简介

    MySQL基础之STRAIGHT JOIN用法简介 引用mysql官方手册的说法: STRAIGHT_JOIN is similar to JOIN, except that the left tab ...

  2. mysql 有报错  ERROR! MySQL is not running, but lock file (/var/lock/subsys/mysql) exists

    sh-4.1# /etc/init.d/mysqld status ERROR! MySQL is not running, but lock file (/var/lock/subsys/mysql ...

  3. Centos安装完MariaDB后启动不了 MySQL is not running, but lock file (/var/lock/subsys/mysql) exists

    [root@admin-node subsys]# service mysql startStarting MySQL. ERROR! [root@admin-node subsys]# servic ...

  4. mysql中INSTR函数的用法

    mysql中INSTR函数的用法 INSTR(字段名, 字符串) 这个函数返回字符串在某一个字段的内容中的位置, 没有找到字符串返回0,否则返回位置(从1开始) SELECT * FROM tblTo ...

  5. MySQL中INSERT的一般用法

    原文链接:http://www.blogjava.net/midnightPigMan/archive/2014/12/15/421406.html MySQL中INSERT的一般用法 INSERT语 ...

  6. mysql 中find_in_set()和in()用法比较

    mysql 中find_in_set()和in()用法比较 在mysql中in可以包括指定的数字,而find_in_set()用于特定的数据类型. find_in_set 函数使用方法 个例子来说:有 ...

  7. MYSQL DATE_FORMAT参数列表及用法

    MYSQL DATE_FORMAT参数列表及用法 主要涉及用法 DATE_SUB(DATE, INTERVAL EXPR TYPE) DATE_FORMAT(DATE,FORMAT) REPLACE( ...

  8. ERROR! MySQL is not running, but lock file (/var/lock/subsys/mysql) exists

    通过service mysql status 命令来查看mysql 的启动状态 报错如下: ERROR! MySQL is not running, but lock file (/var/lock/ ...

  9. Mysql coalesce()函数认识和用法

    Mysql coalesce()函数认识和用法   coalesce()解释:返回参数中的第一个非空表达式(从左向右):    鉴于在mysql中没有nvl()函数, 我们用coalesce()来代替 ...

随机推荐

  1. legend2---开发日志2(注释和函数比较好的写法)

    legend2---开发日志2(注释和函数比较好的写法) 一.总结 一句话总结: 函数用_接意群 注释的关键字用[]括起来 注释的步骤用中文的步骤二字 1.为何以步骤为名写注释? 结构非常清晰 //步 ...

  2. (转)C# 的 String.CompareTo、 Equals和==的比较

    String.CompareTo 语法 public int CompareTo(    string strB) 返回值 小于 0,实例小于参数 strB: 0,实例等于参数 strB: 大于 0, ...

  3. [Maven] guide: maven in 5 minutes

    ran during my bad network connection, it' s more that just 5 minutes. 1. execute "mvn archetype ...

  4. Windows Live Wirter

    安装: 下载: Windows Live Writer (QQ 里) windows live writer 日志服务器发生问题 更新账户信息 从字面"editPost"我们不难看 ...

  5. OnSen UI结合AngularJs打造”美团"APP底部导航栏 --Hybrid App

    1.页面效果图:(点击底部导航按钮,可切换到不同的页面) 演示地址:http://www.nxl123.cn/bokeyuan/2018080301/meiTuanDemo/ 2.项目目录结构 3.核 ...

  6. css图片的全屏显示代码-css3

    <!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...

  7. hdu-4080 Stammering Aliens 字符串hash 模板题

    http://acm.hdu.edu.cn/showproblem.php?pid=4080 求出现次数大于等于n的最长串. #include<iostream> #include< ...

  8. 大div中嵌套小div,点击大div时隐藏,点击小div不隐藏

    给小div添加一个click事件 <div onClick="event.cancelBubble = true">  //小div

  9. Fiddler抓包配置具体步骤

    如何查看手机连接的无线wifi的IP? 打开手机,选择设置->进入设置页面选择WLAN->进入WLAN管理,点击手机已经连接的路由器->点击进入查看,即可看见IP地址 如何查看自己电 ...

  10. Matlab-2:二分法工具箱

    function g=dichotomy(f,tol) %this routine uses bisection to find a zero of user-supplied %continuous ...