【问题标题】:Get unmatched records without using oracle minus except not in在不使用 oracle 减号的情况下获取不匹配的记录,除非在
【发布时间】:2015-08-30 19:44:36
【问题描述】:

实际上我有两个表,每个表都有列名,我只想要 Table2 中没有的结果

Table1
----
Name
---
|A|
|B|
|C|
|D|


Table2
------
|Name|
-----
|A|
|B|

回答 |C| |D|

我可以用减号来做到这一点

select name from table1
minus
select name from table2

 select name from table1 where name
    not in (
    select name from table2) 

但我的经理要求我使用其他替代解决方案而不使用减号,除了,而不是。 有没有办法做到这一点,如果有人可以帮助我,那就太好了。 我需要用 oracle pl/sql 来做

【问题讨论】:

  • 您是说您也不能使用“NOT IN”吗?经理的要求似乎很奇怪。这就是 MINUS 和 NOT IN 的用途。

标签: oracle plsql inner-join outer-join


【解决方案1】:

剩下的一个选项是使用NOT EXISTS

SELECT t1.name 
  FROM table1 t1 
 WHERE NOT EXISTS (SELECT 'X' 
                     FROM table2 t2 
                    WHERE t2.name = t1.name);

更新:使用加入

with table_ as 
(
  select t1.name t1_name, t2.name t2_name
    from table1 t1
    left join table2 t2 
      on t1.name = t2.name)
select t1_name 
  from table_
 where t2_name is null;

或者只是

select t1.name
  from table1 t1
  left join table2 t2 
    on t1.name = t2.name
 where t2.name is null;

【讨论】:

  • 是否可以加入,只是问这对我来说真的很好
  • 用加入更新了答案。
  • 感谢加入对我来说工作正常,我正在寻找这个解决方案
【解决方案2】:

另一种选择是使用外连接,然后过滤第二个表中没有值的行:

with t1 as (select 'A' name from dual union all
            select 'B' name from dual union all
            select 'C' name from dual union all
            select 'D' name from dual),
     t2 as (select 'A' name from dual union all
            select 'B' name from dual)
select t1.name
from   t1
       left outer join t2 on (t1.name = t2.name)
where t2.name is null;

NAME
----
D   
C   

【讨论】:

  • 对不起,Boneist,我在您之后 1 分钟就回答了相同的解决方案。现在我继续删除我的答案并为你的答案投票。
  • 是的,我正在寻找它的工作非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多