MySQL không hỗ trợ toán tử MINUS (EXCEPT), nhưng có thể mô phỏng bằng LEFT JOIN kết hợp điều kiện IS NULL.
Dữ liệu mẫu
mysql> SELECT * FROM Student_detail; +-----------+---------+------------+------------+ | studentid | Name | Address | Subject | +-----------+---------+------------+------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Chandigarh | Literature | | 130 | Ram | Jhansi | Computers | | 132 | Shyam | Chandigarh | Economics | | 133 | Mohan | Delhi | Computers | | 150 | Rajesh | Jaipur | Yoga | | 160 | Pradeep | Kochi | Hindi | +-----------+---------+------------+------------+ mysql> SELECT * FROM Student_info; +-----------+-----------+------------+-------------+ | studentid | Name | Address | Subject | +-----------+-----------+------------+-------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Chandigarh | Literature | | 130 | Ram | Jhansi | Computers | | 132 | Shyam | Chandigarh | Economics | | 133 | Mohan | Delhi | Computers | | 165 | Abhimanyu | Calcutta | Electronics | +-----------+-----------+------------+-------------+
1. Giá trị có trong Student_info nhưng KHÔNG có trong Student_detail
mysql> SELECT studentid FROM student_info LEFT JOIN Student_detail USING(studentid) WHERE student_detail.studentid IS NULL; +-----------+ | studentid | +-----------+ | 165 | +-----------+
2. Giá trị có trong Student_detail nhưng KHÔNG có trong Student_info
mysql> SELECT studentid FROM student_detail LEFT JOIN Student_info USING(studentid) WHERE student_info.studentid IS NULL; +-----------+ | studentid | +-----------+ | 150 | | 160 | +-----------+
Nguyên lý: LEFT JOIN giữ toàn bộ bảng trái, các hàng không khớp sẽ có NULL ở cột bảng phải. Lọc IS NULL lấy phần "chỉ có ở bảng trái".