【问题标题】:why this two select statement doesnt' give same answer为什么这两个选择语句没有给出相同的答案
【发布时间】:2014-02-17 22:06:28
【问题描述】:

我正在使用 oracle 11g。我想知道为什么这两个查询给出不同的答案?

在逻辑上它们是相同的:

   select * from tableA where 
   exists (select * from tableB where tableA.ID != tableB.ID);

   select * from tableA where 
   not exists (select * from tableB where tableA.ID = tableB.ID);

在第一个中,我选择了所有不存在的东西。

在第二个中,我没有选择所有存在的东西。

注意(“存在”改为“不存在”)和(“!=”改为“=”)

看起来一样吧?但他们给出了完全不同的答案

【问题讨论】:

    标签: oracle oracle11g


    【解决方案1】:

    这个语句可能会返回 A 中的所有值:

    select *
    from tableA 
    where exists (select * from tableB where tableA.ID != tableB.ID);
    

    只有当它与TableB 中的所有 行在ID 中具有非NULL 值的行相同时才会匹配。因此,如果TableB 至少有两行具有不同的ids,则将返回tableA 中的所有行。

    此声明:

    select *
    from tableA 
    where not exists (select * from tableB where tableA.ID = tableB.ID);
    

    是说TableB 中没有与TableA 中的id 匹配的id。这将是您 99% 的时间想要的。

    【讨论】:

    • +1,但第一种情况有可能超过 1 行:第二行必须在 ID 字段中包含 null
    【解决方案2】:

    第一条语句返回不同于任何 B 值的 A 值。 第二条语句返回不同于所有 B 值的 A 值。

    【讨论】:

      猜你喜欢
      • 2021-11-18
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      • 2013-06-13
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      相关资源
      最近更新 更多