ใช้ IN Keyword กรองข้อมูลด้วย Subquery เนื่องจากผลลัพธ์ Subquery ใช้ได้เหมือน List of Values:
ตัวอย่าง
ข้อมูลจาก 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)
กรองด้วย IN + Subquery
mysql> SELECT * from customers WHERE customer_id IN (Select customer_id from reservations); +-------------+----------+ | Customer_Id | Name | +-------------+----------+ | 1 | Rahul | | 2 | Yashpal | | 3 | Gaurav | +-------------+----------+ 3 rows in set (0.00 sec)
คืนลูกค้าที่มีการจอง (Customer_id อยู่ใน Subquery)