当前位置:网站首页>mysql中exists的用法详解
mysql中exists的用法详解
2022-07-28 22:23:00 【m0_54861649】
前言
在日常开发中,用mysql进行查询的时候,有一个比较少见的关键词exists,我们今天来学习了解一下这个
exists这个sql关键词的用法,这样在工作中遇到一些特定的业务场景就可以有更加多样化的解决方案
语法解释
语法
SELECT column1 FROM t1 WHERE [conditions] and EXISTS (SELECT * FROM t2 );
说明
- 括号中的子查询并不会返回具体的查询到的数据,只是会返回true或者false,如果外层sql的字段在子查询中存在则返回true,不存在则返回false
- 即使子查询的查询结果是null,只要是对应的字段是存在的,子查询中则返回true,下面有具体的例子
执行过程
1、首先进行外层查询,在表t1中查询满足条件的column1
2、接下来进行内层查询,将满足条件的column1带入内层的表t2中进行查询,
3、如果内层的表t2满足查询条件,则返回true,该条数据保留
4、如果内层的表t2不满足查询条件,则返回false,则删除该条数据
5、最终将外层的所有满足条件的数据进行返回
贴个链接,mysql官方对于这个命令的说明: https://dev.mysql.com/doc/refman/8.0/en/exists-and-not-exists-subqueries.html;喜欢看英文原版说明的可以来这里看一下
使用案例
环境准备
?? mysql版本: 8.0.28
?? 数据库表设计:
学生表: t_student
CREATE TABLE
t_student(idint unsigned NOT NULL AUTO_INCREMENT,namevarchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT ‘’ COMMENT ‘学生姓名’,ageint NOT NULL COMMENT ‘年龄’,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT=‘学生表’;
导入部分数据
INSERT INTO `t_student` (`id`, `name`, `age`)
VALUES
(1, '小张', 10),
(2, 'chenille', 13),
(3, '小王', 15),
(4, '小米', 11),
(5, 'dong', 13),
(6, 'xi', 12),
(7, 'chenille', 13),
(8, '小王地方', 15),
(9, '米来', 11),
(10, 'dong', 13),
(11, '呵呵', 12),
(12, 'chenille', 13),
(13, '小赵', 15),
(14, '小米-0', 11),
(15, 'bei', 13),
(16, 'xi-xx', 12),
(17, 'chenille', 13),
(18, '小王-hehe', 15),
(19, '小米-qian', 11),
(20, 'dong', 13),
(21, 'xi', 12),
(22, 'chenille', 13),
(23, '小王-1', 15),
(24, '小米-2', 11),
(25, 'dong-3', 13),
(26, 'xi-0', 12),
(27, 'chenille-4', 13),
(28, '小王-4', 15),
(29, '小米-7', 11),
(30, 'dong-1', 13),
(31, 'xi-5', 12),
(32, '貔貅', 10),
(33, '耄耋', 12),
(34, '饕餮', 9),
(35, '龙', 13),
(36, '青牛', 12);
班级学生表:t_class_student
CREATE TABLE
t_class_student(idint unsigned NOT NULL AUTO_INCREMENT,student_idint NOT NULL COMMENT ‘学生ID’,class_idint NOT NULL COMMENT ‘班号’,class_namevarchar(100) DEFAULT ‘’ COMMENT ‘班级名称’,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT=‘班级学生表’;
导入部分数据
INSERT INTO `t_class_student` (`id`, `student_id`, `class_id`, `class_name`)
VALUES
(1, 1, 1, '一年级1班'),
(2, 2, 1, '一年级1班'),
(3, 3, 1, '一年级1班'),
(4, 4, 1, '一年级1班'),
(5, 5, 1, '一年级1班'),
(6, 6, 1, '一年级1班'),
(7, 7, 1, '一年级1班'),
(8, 8, 1, '一年级1班'),
(9, 9, 1, '一年级1班'),
(10, 10, 1, '一年级1班'),
(11, 11, 2, '一年级2班'),
(12, 12, 2, '一年级2班'),
(13, 13, 2, '一年级2班'),
(14, 14, 2, '一年级2班'),
(15, 15, 2, '一年级2班'),
(16, 16, 2, '一年级2班'),
(17, 17, 2, '一年级2班'),
(18, 18, 2, '一年级2班'),
(19, 19, 2, '一年级2班'),
(20, 20, 2, '一年级2班'),
(21, 21, 3, '二年级2班'),
(22, 22, 3, '二年级2班'),
(23, 23, 3, '二年级2班'),
(24, 24, 3, '二年级2班'),
(25, 25, 3, '二年级2班'),
(26, 26, 3, '二年级2班'),
(27, 27, 3, '二年级2班'),
(28, 28, 3, '二年级2班'),
(29, 29, 3, '二年级2班'),
(30, 30, 3, '二年级2班'),
(31, 31, 4, '三年级1班');
(32, 32, 4, null);
常用查询
已分配班级的学生名单 ??
select * from t_student as s where exists (select student_id from t_class_student where student_id = s.id);
未分配班级的学生名单 ??
select * from t_student as s where not exists (select student_id from t_class_student where student_id = s.id);
已分配 三年级1班 的学生名单 ??
select * from t_student as s where exists (select student_id from t_class_student where student_id = s.id and class_id = 4);
已分配 并且班级是 一年级1班 和 一年级2班 的学生名单 ??
select * from t_student as s where exists (select student_id from t_class_student where student_id = s.id and class_id in (1, 2) );
查询到的字段为null,但是子查询返回的结果为true ??
select * from t_student as s where exists (select class_name from t_class_student where student_id = s.id and class_id = 4);
查询全部学生名单 ??
select * from t_student as s where exists (select student_id from t_class_student where 1=1);
已分配 三年级1班 的并且年龄大于10岁的学生名单 ??
select * from t_student as s where age > 10 and exists (select student_id from t_class_student where student_id = s.id and class_id = 4);
exists与in的效率比较
上面的这些查询其实也可以通过 in 关键字来实现,下面我们写一下 in 关键字对应的查询语句,
通过 in 实现已分配班级的学生名单 ??
select * from t_student as s where id in (select student_id from t_class_student where student_id = s.id);
通过 in 实现未分配班级的学生名单 ??
select * from t_student as s where id not in (select student_id from t_class_student where student_id = s.id);
下面我们来分析一下这两个关键字使用效率到底那个更高呢?
循环嵌套查询执行原理
?? 循环由外向内,外层循环执行一次,内层循环则需要完整的执行一次,内层执行执行完后返回执行结果,外层循环继续执行,直到外层循环完全执行完成
循环优化策略
?? 有了上面的执行原理的说明,我们明白了一个道理:内层循环次数的多少不会影响到外层的次数,但是外层循环的次数直接会影响到内层循环的次数,外层循环每多一次,内层循环就需要多完整的一次循环,所以我们优化的目标其实就是使外层的循环次数尽量少,总结来说:小表驱动大表。小表就是外层循环,大表就是内层循环,也就是尽量减少外层循环的次数
exists和in查询原理的区别
?? exists : 外表先进行循环查询,将查询结果放入exists的子查询中进行条件验证,确定外层查询数据是否保留
?? in : 先查询内表,将内表的查询结果当做条件提供给外表查询语句进行比较
结论
通过上面的优化策略分析和exists和in的查询原理的分析,将这两块内容结合起来其实就得出了我们想要的一个结论:
- 外层小表,内层大表(或者将sql从左到由来看:左面小表,右边大表): exists 比 in 的效率高
- 外层大表,内层小表(或者将sql从左到由来看:左面大表,右边小表): in 比 exists 的效率高
- 参考资料
https://www.bilibili.com/video/BV1V64y1q7yispm_id_from=333.337.search-card.all.click
总结
上面我们简单介绍了一下平时我们比较少用到的一个exists的关键字,通过一些使用的实例,大家也可以基本上了解了它的使用方法,在以后的日常工作中,我们在碰到一些查询问题的时候,这个时候就有了更加多样化的选择方案啦,希望这篇文章对大家有帮助,谢谢
边栏推荐
- Leetcode63. 不同路径 II
- websocket心跳机制(保活机制)
- 【C】逆序字符串(俩种递归思路)
- Summary of wrong questions of software designers
- Genomic DNA isolation Worthington ribonuclease A
- 【微服务】Nacos集群搭建以及加载文件配置
- The failure rate is as high as 80%. How to correctly complete the strategic planning of digital transformation?
- [detailed and super simple] how to use websocket links
- Real time data warehouse: Netease strictly selects the practice of real-time data warehouse based on Flink
- What do you need to bring with you for the NPDP exam? Stationery carrying instructions
猜你喜欢

实时数仓:美团基于Flink的实时数仓建设实施

Worthington -- Specification of Worthington trypsin inhibitor

Eight performance analysis indicators of effective supply chain management (Part 1)

PowerCL 批量创建及管理虚拟交换机

以JSP为视图解析器搭建SSM项目

EN 1935 building hardware. Single axis hinge - CE certification

curl (7) Failed connect to localhost8080; Connection refused

【C】 Drink soda and find a single dog

Explanation of history and chemical properties of Worthington ribonuclease B

有效供应链管理的八大绩效分析指标(上)
随机推荐
【C】替换空格,宏实现整数的二进制奇偶位交换
Doip test development practice
NAT如何配置地址转换
With the help of rpa+lcap, the enterprise treasurer management can be upgraded digitally
Use pytoch to quickly train the network model
Best practices for migration of kingbasees v8.3 to v8.6 of Jincang database (3. Kingbasees migration capability support system)
Android studio connects to MySQL and completes simple login and registration functions
GhostNets on Heterogeneous Devices via Cheap Operations
跳表的原理
mysql索引失效的常见9种原因详解
[TA frost wolf _may- "hundred people plan"] art 2.2 model basis
PowerCLi VMware vCenter 通过自建的PXE Server一键批量部署常规New-VM
[detailed and super simple] how to use websocket links
EN 1873 assembly accessories for roofing - plastic single roof lamps - CE certification
1-4 类的复习
EN 1873屋面用装配附件.塑料单个屋面灯—CE认证
CMake 基础学习
Zibo station construction guide (aigler)
Compose 的声明式代码如此简洁?
Leetcode60. permutation sequence