sql - Oracle MINUS 两种方式(获得选择之间的差异)

标签 sql oracle

我想找到两个 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/

相关文章:

sql - 不是 GROUP BY 表达式

c# - SQLDataReader 运行速度太慢取决于标准

mysql - 用于检查 JSON 数组中是否存在至少一项的 SQL 正则表达式

sql - 如何使用 VBA 从数据库中保存数百万条记录?

javascript - 甲骨文顶点 : Color the values in column of tabular form

database - 事务处理后脚本不起作用

java - 映射减少输出到oracle数据库?

sql - 查找一列中与另一列中的特定行值相关的相同行数

sql - Snowflake 脚本 - 如何访问调用存储过程时传递的变量

java - 在 jooq 中访问 sql-array 项目