首页 > 解决方案 > 在 SQL 查询中执行 MINUS 时忽略列

问题描述

我已经写了查询:

Select distinct a,b from t1 minus Select distinct a,b from t2.

这里 t1 和 t2 是两个表。我想要在 t1 但不在 t2 中出现的 a 和 b 的不同值。所以我使用减号运算符。我想要 a 和 b 的值,但我知道在某些情况下, t1 和 t2 中的 b 值可能不同。这将导致 t1 和 t2 中都存在 a 和 b 的值,因为如果 b 的值在两个表中都不匹配,则不会发生减号。我怎样才能成功地做到这一点?

如何获取表 t1 中存在但表 t2 中不存在的 a 和 b 的值,即使在某些情况下 b 的值可能在两个表中都不匹配?

    table1:                              table2:
column1  column2                    column1   column2
  1      a                             1         c
  2      b                             3         d

在这种情况下,我只想要值 (2,b)。我不希望 (1,a) 因为 1 也出现在 table2 中。

标签: sqloracle

解决方案


开始not exists

select distinct. . .
from t1
where not exists (select 1 from t2 where t2.a = t1.a and t2.b = t1.b);

根据您的描述,您可能只想比较a

select distinct a, b
from t1
where not exists (select 1 from t2 where t2.a = t1.a);

推荐阅读