Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

MySQL ประเมิน EXISTS กับ Subquery ที่คืน 0 แถว อย่างไร


ถ้า Subquery ที่ใช้กับ EXISTS คืน 0 แถว EXISTS จะเป็น FALSE และ MySQL คืน Empty Set ตัวอย่างใช้ตาราง Customers, Reservations:

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)

Subquery ที่ customer_id = 4 ไม่มีใน Reservations:

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

ผล: EXISTS FALSE คืน Empty Set