MySQL 如何計算我們使用帶子查詢(不返回任何行)的 EXISTS 運算子的情況?


如果與 EXIST 運算子一起使用的子查詢不返回任何行,則 EXIST 表示式返回 FALSE,並且 MySQL 返回空集作為輸出。藉助以下來自表“Customers”的簡單示例,您可對此有一個認識 -

mysql> Select * from Customers;
+-------------+----------+
| Customer_Id | Name     |
+-------------+----------+
|           1 | Rahul    |
|           2 | Yashpal  |
|           3 | Gaurav   |
|           4 | Virender |
+-------------+----------+
4 rows in set (0.00 sec)

mysql> Select * from Reservations;
+------+-------------+------------+
| ID   | Customer_id | Day        |
+------+-------------+------------+
|    1 |           1 | 2017-12-30 |
|    2 |           2 | 2017-12-28 |
|    3 |           2 | 2017-12-29 |
|    4 |           1 | 2017-12-25 |
|    5 |           3 | 2017-12-26 |
+------+-------------+------------+
5 rows in set (0.00 sec)

如下所示的 MySQL 查詢包含帶 EXIST 運算子的子查詢,該子查詢不返回任何行。在這種情況下,EXIST 表示式返回 FALSE,因此結果集為空集。

mysql> Select Name from Customers WHERE EXISTS (SELECT * FROM Reservations WHERE customer_id = 4);
Empty set (0.00 sec)

更新於: 2020 年 6 月 22 日

109 次瀏覽

開啟你的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.