如果我們使用返回 0 行的子查詢和 EXISTS 運算子,MySQL 如何評估結果?
如果與 EXIST 運算子一起使用的子查詢未返回任何行,則 EXIST 表示式返回 FALSE,而 MySQL 返回空集作為輸出。利用從表“客戶”中獲取以下資料的簡單示例,可以理解這一點——
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)
廣告