为什么这两个选择语句没有给出相同的答案

时间:2014-02-17 02:52:55

标签: oracle oracle11g

我正在使用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);

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

在第二个我没有选择存在的一切。

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

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

2 个答案:

答案 0 :(得分:3)

此语句可能会返回A:

中的所有值
select *
from tableA 
where exists (select * from tableB where tableA.ID != tableB.ID);

一行无法匹配的唯一时间是与TableB所有行中{em} ID中具有非NULL值的行相同。因此,如果TableB至少有两行具有不同的id s,则会返回tableA中的所有行。

本声明:

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

id中的TableBTableA中的ID匹配。这将是99%的时间你想要的。

答案 1 :(得分:0)

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

相关问题