如何获得两个结果集的设置差异?

假设我有一个结果集(每列只有一列):

result1:
'a'
'b'
'c'

result2:
'b'
'c'

我想减去result1中的内容2:result1 -result 2,使其等于:

 difference of result1 - result2:
 'a'
有帮助吗?

解决方案

要执行Result1 -Result2,您可以将Result1加入Result2,并且仅在Result1中存在输出项。例如:

SELECT DISTINCT result1.column
FROM result1 LEFT JOIN result2 ON result1.column = result2.column
WHERE result2.column IS NULL

注意不是集合 不同之处, ,并且不会输出结果2中不存在的项目1。它设置了 减法.

也可以看看: 有关博客文章的Web存档版本.

其他提示

如果你想要东西 result1 不在 result2, , 关于什么:

SELECT distinct result1
FROM t1 
WHERE result1 NOT IN (select distinct result2 from t2);

或者:

SELECT distinct result
from t1 t
where NOT EXISTS (select 1 from t2 where result2 = t.result1)

注意:如果 result1result2 然后上面的查询将返回一个空集(它们不会在 result2 不在 result1)因此它们不是设定的差异,但也可能有用(可能比外部联接更有效)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top