Mysql exists用法小結

EXISTS用于檢查子查詢是否至少會返回一行數據,該子查詢實際上并不返回任何數據,而是返回值True或False。

EXISTS 指定一個子查詢,檢測行的存在。語法:EXISTS subquery。參數 subquery 是一個受限的 SELECT 語句 (不允許有 COMPUTE 子句和 INTO 關鍵字)。結果類型為 Boolean,如果子查詢包含行,則返回 TRUE。文章源自四五設計網-http://www.133122.cn/44069.html

在mysql中,exists用于檢查子查詢是否至少會返回一行數據,該子查詢實際上并不返回任何數據,而是返回true或false,語法為“SELECT 字段 FROM table WHERE EXISTS (subquery);”。文章源自四五設計網-http://www.133122.cn/44069.html

語法:

SELECT 字段 FROM table WHERE EXISTS (subquery);文章源自四五設計網-http://www.133122.cn/44069.html

參數:

subquery是一個受限的SELECT語句(不允許有COMPUTE子句和INTO關鍵字)文章源自四五設計網-http://www.133122.cn/44069.html

示例:

SELECT * FROM A WHERE EXISTS (SELECT 1 FROM B WHERE B.id = A.id);文章源自四五設計網-http://www.133122.cn/44069.html

EXISTS執行順序:文章源自四五設計網-http://www.133122.cn/44069.html

1、首先執行一次外部查詢,并緩存結果集,如 SELECT * FROM A文章源自四五設計網-http://www.133122.cn/44069.html

2、遍歷外部查詢結果集的每一行記錄R,代入子查詢中作為條件進行查詢,如 SELECT 1 FROM B WHERE B.id = A.id文章源自四五設計網-http://www.133122.cn/44069.html

3、如果子查詢有返回結果,則EXISTS子句返回TRUE,這一行R可作為外部查詢的結果行,否則不能作為結果文章源自四五設計網-http://www.133122.cn/44069.html

示例如下:文章源自四五設計網-http://www.133122.cn/44069.html

假設現在有三張表:

student:學生表,其中有字段sno為學號,sname為學生姓名

course:課程表,其中有字段cno為課程號,cname為課程名稱

student_course_relation:選課表,記錄學生選擇了哪些課程,其中有字段sno為學號,cno為課程號

下面通過幾個示例來說明一下EXISTS和NOT EXISTS的用法,及其與IN和NOT IN的區別

1、在子查詢中使用NULL,仍然返回結果集

下面三種情況返回數據相同,都會返回student表的所有數據:

1
2
3
select * from student;?
select * from student where exists (select 1);?
select * from student where exists (select null);

2、EXISTS子查詢返回的是一個布爾值true或false

EXISTS用于檢查子查詢是否至少會返回一行數據,該子查詢實際上并不返回任何數據,而是返回布爾值true或false,EXISTS指定一個子查詢,檢測行的存在。

EXISTS只在乎子查詢中是否有記錄,與具體的結果集無關,所以下面示例中,子查詢中的select sno也可以換成select cno或者select 1,查詢出的結果集是一樣的。

查詢所有選修了課程號為3的學生:

1
2
3
4
5
6
select * from student a?
where exists (select sno from student_course_relation b where b.cno=3 and b.sno=a.sno);?
select * from student a?
where exists (select cno from student_course_relation b where b.cno=3 and b.sno=a.sno);?
select * from student a?
where exists (select 1 from student_course_relation b where b.cno=3 and b.sno=a.sno);

補充示例

一張活動配置主表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用法小結的詳細內容

繼續閱讀
我的微信
微信掃一掃
weinxin
我的微信
惠生活福利社
微信掃一掃
weinxin
我的公眾號
 
  • 本文由 四五設計網小助手 發表于 2024年3月29日10:54:39
  • 轉載請務必保留本文鏈接:http://www.133122.cn/44069.html

發表評論

匿名網友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖動滑塊以完成驗證