Các toán tử ALL, ANY, SOME, IN so sánh giá trị với mọi giá trị trả về bởi subquery. Tất cả phải theo sau một toán tử so sánh.
Cú pháp
Operand comparison_operator ALL (subquery) Operand comparison_operator ANY (subquery) Operand comparison_operator SOME (subquery) Operand comparison_operator IN (subquery)
Ví dụ
Dữ liệu bảng Customers và Reservations:
mysql> Select * from Customers; +-------------+----------+ | Customer_Id | Name | +-------------+----------+ | 1 | Rahul | | 2 | Yashpal | | 3 | Gaurav | | 4 | Virender | +-------------+----------+ 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 | +------+-------------+------------+
ALL (khác TẤT CẢ giá trị subquery):
mysql> SELECT customer_id from Customers
WHERE customer_id <> ALL(Select customer_id from reservations);
+-------------+
| customer_id |
+-------------+
| 4 |
+-------------+
1 row in set (0.00 sec)
Chỉ Virender (id=4) KHÔNG có trong Reservations.
ANY (bằng BẤT KỲ giá trị subquery):
mysql> SELECT customer_id from customers
WHERE customer_id = ANY(Select customer_id from reservations);
+-------------+
| customer_id |
+-------------+
| 1 |
| 2 |
| 3 |
+-------------+
3 rows in set (0.00 sec)
SOME (tương đương ANY):
mysql> SELECT customer_id from customers
WHERE customer_id = Some(Select customer_id from reservations);
+-------------+
| customer_id |
+-------------+
| 1 |
| 2 |
| 3 |
+-------------+
3 rows in set (0.00 sec)
IN (tương đương = ANY):
mysql> SELECT customer_id from customers
WHERE customer_id IN (Select customer_id from reservations);
+-------------+
| customer_id |
+-------------+
| 1 |
| 2 |
| 3 |
+-------------+
3 rows in set (0.00 sec)
Tóm tắt: IN = ANY = SOME (khớp ít nhất 1), ALL (khớp tất cả).