我想找到两个 select
语句中的所有行,其中行属于(所有列匹配)其中一个 select 语句但不是两个 em>.
我知道如何在一个方向上实现这一点
select foo from foobar where bar = 1
minus
select foo from foobar where bar = 2;
这将返回第一个 select 语句中出现 foo 的所有行,但不是最后一个,但不是其他方式。
我知道我能做到
(select foo from foobar where bar = 1
minus
select foo from foobar where bar = 2)
union
(select foo from foobar where bar = 2
minus
select foo from foobar where bar = 1);
返回所需的结果。但这是很多重复。是否有像 minus
这样的双向运算符?
此外,我想在结果中添加标识符,指示它属于哪个选择。但仅此一项就会使所有行都不同。
最佳答案
SET 运算符只是
UNION All distinct rows selected by either query
UNION ALL All rows selected by either query, including all duplicates
INTERSECT All distinct rows selected by both queries
MINUS All distinct rows selected by the first query but not the second
你可以使用 intersect 但 select 的数量几乎是一样的
(select foo from foobar where bar = 1
union
select foo from foobar where bar = 2)
minus
(select foo from foobar where bar = 1
intesect
select foo from foobar where bar = 2)
关于sql - Oracle MINUS 两种方式(获得选择之间的差异),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44702450/