Mysql exists用法小结

吾爱主题 阅读:121 2024-04-02 07:58:09 评论:0

简介

EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False。

EXISTS 指定一个子查询,检测行的存在。语法:EXISTS subquery。参数 subquery 是一个受限的 SELECT 语句 (不允许有 COMPUTE 子句和 INTO 关键字)。结果类型为 Boolean,如果子查询包含行,则返回 TRUE。

示例

一张活动配置主表activity_main,通过act_code来唯一标明一场活动,活动举办地点适配表activity_area,通过act_code与主表进行关联,活动奖品表activity_sku,通过act_code与主表进行关联。

活动主表

?
1 2 3 4 5 6 7 CREATE TABLE `activity_main` ( `id` bigint (20) NOT NULL AUTO_INCREMENT, `act_code` varchar (255) NOT NULL COMMENT '活动代码' , `act_name` varchar (255) NOT NULL COMMENT '活动名称' , PRIMARY KEY (`id`), UNIQUE KEY `uniq_code` (`act_code`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE =utf8mb4_0900_ai_ci COMMENT= '活动主表'

活动在哪些网站举办的适配表

?
1 2 3 4 5 6 CREATE TABLE `activity_area` (   `id` bigint (20) NOT NULL AUTO_INCREMENT,   `act_code` varchar (255) NOT NULL COMMENT '活动代码' ,   `area` varchar (255) NOT NULL COMMENT '参与此活动的网站' ,   PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE =utf8mb4_0900_ai_ci COMMENT= '活动适配的网站列表'

活动奖品表

?
1 2 3 4 5 6 CREATE TABLE `activity_sku` (   `id` bigint (20) NOT NULL AUTO_INCREMENT,   `act_code` varchar (255) NOT NULL COMMENT '活动代码' ,   `sku` varchar (255) NOT NULL COMMENT '活动赠送的商品' ,   PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE =utf8mb4_0900_ai_ci COMMENT= '活动赠品表'

比较使用 EXISTS 和 IN 的查询
这个例子比较了两个语义类似的查询。第一个查询使用 IN 而第二个查询使用 EXISTS。注意两个查询返回相同的信息。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 # 查询体重秤 select * from activity_main where act_code in ( select act_code from activity_sku where sku = '翎野君的体脂称' )   # 查询体重秤 select * from activity_main a where exists ( select 1 from activity_sku b where a.act_code = b.act_code and b.sku = '翎野君的体脂称' )   # 模糊查询B-BEKO英国婴儿推车 select * from activity_main where act_code in ( select act_code from activity_sku where sku like '%B-BEKO%' )   # 模糊查询B-BEKO英国婴儿推车 select * from activity_main a where exists ( select 1 from activity_sku b where a.act_code = b.act_code and b.sku like '%B-BEKO%' )   # 查询在博客园举办的活动 select * from activity_main where act_code in ( select act_code from activity_area where area = '博客园' )   # 查询在博客园举办的活动 select * from activity_main a where exists ( select 1 from activity_area b where a.act_code = b.act_code and b.area = '博客园' )     # 在博客园举办活动且活动奖品为华为手机的活动信息 select * from activity_main where act_code in ( select act_code from activity_area where area = '博客园' and act_code in ( select act_code from activity_sku where sku = '华为P30Pro' ))     # 内层的exists语句只在当前 where 语句中生效,最终是否返回,要根据最外层的exists判断,如果是 true (真)就返回到结果集,为 false (假)丢弃。 select * from activity_main a where exists ( select 1 from activity_area b where a.act_code = b.act_code and b.area = '博客园' and exists ( select 1 from activity_sku c where a.act_code = c.act_code and c.sku = '华为P30Pro' ) )

以上就是Mysql exists用法小结的详细内容,更多关于Mysql exists用法的资料请关注服务器之家其它相关文章!

原文链接:https://segmentfault.com/a/1190000038464558

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容