query_expression_bodyEXCEPT [ALL | DISTINCT]query_expression_body[EXCEPT [ALL | DISTINCT]query_expression_body] [...]query_expression_body: See Section 15.2.14, “Set Operations with UNION, INTERSECT, and EXCEPT”
EXCEPT 限制第一个查询块的结果只能访问第二个查询块中找不到的行。与 UNION 和 INTERSECT),任一查询块都可以使用 SELECT TABLE或 VALUES以下示例为a,b,c 定义在 第 15.2.8 节,“INTERSECT Clause”,EXCEPT limits the result from the
first query block to those rows which are (also) not found in the
second. As with UNION and
INTERSECT, either query block can
make use of any of SELECT,
TABLE, or
VALUES. An example using the tables
a, b, and
c defined in Section 15.2.8, “INTERSECT Clause”, is
shown here:
mysql>+------+------+ | m | n | +------+------+ | 2 | 3 | +------+------+ 1 row in set (0.00 sec) mysql>TABLEa EXCEPT TABLE b;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)
与 UNION 和 INTERSECT,如果既不指定 DISTINCT指定 ALL,默认为 DISTINCT。As with UNION and
INTERSECT, if neither
DISTINCT nor ALL is
specified, the default is DISTINCT.
DISTINCT会移除关系两侧的重复,如图所示:DISTINCT removes duplicates found on either
side of the relation, as shown here:
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效果。)(The first statement has the same effect as TABLE c
EXCEPT TABLE a.)
与UNION 或 INTERSECT,EXCEPT 是 非交换——即结果取决于操作数的顺序,如图所示:Unlike UNION or INTERSECT,
EXCEPT is not
commutative—that is, the result depends on the order of the
operands, as shown here:
mysql>TABLE a EXCEPT TABLE c;+------+------+ | m | n | +------+------+ | 1 | 2 | | 2 | 3 | +------+------+ 2 rows in set (0.00 sec) mysql>TABLE c EXCEPT TABLE a;+------+------+ | m | n | +------+------+ | 1 | 3 | +------+------+ 1 row in set (0.00 sec)
与UNION,要比较的结果集必须具有相同的列数。结果集的列类型也如 UNION。As with UNION, the result sets to be compared
must have the same number of columns. Result set column types are
also determined as for UNION.