EXCEPT 返回两个结果集的差
INTERSECT 返回两个结果集的交集
举例说明:
mysql> CREATE TABLE t1 (id INT);
Query OK, 0 rows affected (0.00 sec)mysql> CREATE TABLE t2 (id INT);
Query OK, 0 rows affected (0.01 sec)mysql> INSERT INTO t1 VALUES (1),(2),(3),(4),(5),(6);
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0mysql> INSERT INTO t2 VALUES (1),(2),(3);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0mysql> select * from t1 EXCEPT select * from t2;
+------+
| i |
+------+
| 4 |
| 5 |
| 6 |
+------+
3 rows in set (0.00 sec)mysql> select * from t1 INTERSECT select * from t2 ;
+------+
| i |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
注:MySQL 8.0不支持该语法。