簡介
EXISTS用于檢查子查詢是否至少會返回一行數(shù)據(jù),該子查詢實際上并不返回任何數(shù)據(jù),而是返回值True或False。
EXISTS 指定一個子查詢,檢測行的存在。語法:EXISTS subquery。參數(shù) subquery 是一個受限的 SELECT 語句 (不允許有 COMPUTE 子句和 INTO 關(guān)鍵字)。結(jié)果類型為 Boolean,如果子查詢包含行,則返回 TRUE。
示例
一張活動配置主表activity_main,通過act_code來唯一標(biāo)明一場活動,活動舉辦地點適配表activity_area,通過act_code與主表進行關(guān)聯(lián),活動獎品表activity_sku,通過act_code與主表進行關(guān)聯(lián)。
活動主表
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='活動主表'
活動在哪些網(wǎng)站舉辦的適配表
CREATE TABLE `activity_area` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`act_code` varchar(255) NOT NULL COMMENT '活動代碼',
`area` varchar(255) NOT NULL COMMENT '參與此活動的網(wǎng)站',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='活動適配的網(wǎng)站列表'
活動獎品表
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。注意兩個查詢返回相同的信息。
# 查詢體重秤
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'
))
# 內(nèi)層的exists語句只在當(dāng)前where語句中生效,最終是否返回,要根據(jù)最外層的exists判斷,如果是 true(真)就返回到結(jié)果集,為 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用法小結(jié)的詳細內(nèi)容,更多關(guān)于Mysql exists用法的資料請關(guān)注腳本之家其它相關(guān)文章!
您可能感興趣的文章:- MySQL中in與exists的使用及區(qū)別介紹
- 對比分析MySQL語句中的IN 和Exists
- mysql exists與not exists實例詳解
- MySQL exists 和in 詳解及區(qū)別
- mySQL中in查詢與exists查詢的區(qū)別小結(jié)
- MySQL關(guān)于exists的一個bug
- 安裝mysql出錯”A Windows service with the name MySQL already exists.“如何解決
- MySQL的子查詢中FROM和EXISTS子句的使用教程
- MYSQL IN 與 EXISTS 的優(yōu)化示例介紹
- mysql not in、left join、IS NULL、NOT EXISTS 效率問題記錄
- UCenter info: MySQL Query Error SQL:SELECT value FROM [Table]vars WHERE noteexists