除了它存在于其他 2 个表中

EXCEPT it exits in 2 other tables

我需要从 table 产品中获取所有值,除了它们存在于其他 2 个 table 产品中。这个查询 acceptable 使用 2 EXCEPTS 吗?这应该以不同的方式完成吗?

CREATE table missing_values
AS
select post
from product
EXCEPT
select post
from product_table_a
EXCEPT
select post
from product_table_b
;

正如 MatBailie 所说,您应该尝试查询,看看是否能得到您想要的结果。

但如果这不起作用,你可以用双 not exists

解决它

这意味着 p1p2p3 中都找不到。

SELECT post
FROM product p1
WHERE not exists (SELECT p2.post                       
                  FROM product_table_a p2
                  WHERE p1.post = p2.post)
AND   not exists (SELECT p3.post                       
                  FROM product_table_b p3
                  WHERE p1.post = p3.post)

另外,这可能是一种更有效的方法

SELECT post
FROM product p1
left join product_table_a p2
   on p1.post = p2.post
left join product_table_b p3
   on p1.post = p3.post
WHERE p2.post is null
and   p3.post is null

A - B - C 等同于 A - (B + c):

SELECT post
FROM product
EXCEPT
(
  SELECT post
  FROM product_table_a
  UNION ALL
  SELECT post
  FROM product_table_b
) AS sum;