如果我们将 EXISTS 运算符与不返回任何行的子查询一起使用,MySQL 将如何评估?
mysqlmysqli database
如果与 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)