Documentation Home

13.2.10.5 EXCEPT 子句

SELECT ...
EXCEPT [ALL | DISTINCT] SELECT ...
[EXCEPT [ALL | DISTINCT] SELECT ...]

EXCEPT将第一个语句的结果限制为在第二个SELECT语句中(也)没有找到的那些行。此处显示了使用表a,b第 13.2.10.4 节“INTERSECT 子句”c中定义的示例:

mysql> TABLE a EXCEPT TABLE b;
+------+------+
| m    | n    |
+------+------+
|    2 |    3 |
+------+------+
1 row in set (0.00 sec)

mysql> TABLE a EXCEPT TABLE c;
+------+------+
| m    | n    |
+------+------+
|    1 |    2 |
|    2 |    3 |
+------+------+
2 rows in set (0.00 sec)

mysql> TABLE b EXCEPT TABLE c;
+------+------+
| m    | n    |
+------+------+
|    1 |    2 |
+------+------+
1 row in set (0.00 sec)

UNIONand 一样INTERSECT,如果 未指定DISTINCTnor ALL,则默认为DISTINCT.

DISTINCT删除关系两侧的重复项,如下所示:

mysql> TABLE c EXCEPT DISTINCT TABLE a;
+------+------+
| m    | n    |
+------+------+
|    1 |    3 |
+------+------+
1 row in set (0.00 sec)

mysql> TABLE c EXCEPT ALL TABLE a;
+------+------+
| m    | n    |
+------+------+
|    1 |    3 |
|    1 |    3 |
+------+------+
2 rows in set (0.00 sec)

(第一个语句与 具有相同的效果TABLE c EXCEPT TABLE a。)

UNION一样,要比较的结果集必须具有相同的列数。结果集列类型也被确定为 for UNION

EXCEPT在 MySQL 8.0.31 中添加。