MySQL 如果我们在子查询中使用EXISTS运算符但返回零行,会如何评估
如果与EXIST运算符一起使用的子查询未返回任何行,则表达式EXIST返回FALSE,MySQL将空集作为输出返回。这可以利用使用以下来自表“Customers”的数据的简单示例来理解 –
mysql > Select * from Customers;
+------------- + ----------+
| Customer_Id | Name |
+------------- + ----------+
| 1 | Rahul |
| 2 | Yashpal |
| 3 | Gaurav |
| 4 | Virender |
+------------- + ----------+
4行在集合中(0.00秒)
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行在一组中(0.00秒)
下面的MySQL查询具有使用EXIST运算符的子查询,该子查询未返回任何行。在这种情况下,EXIST表达式返回FALSE,因此结果集为空集。
mysql > Select Name from Customers WHERE EXISTS (SELECT * FROM Reservations WHERE customer_id = 4);
Empty set (0.00 sec)
阅读更多:MySQL 教程
极客教程