Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oracle 为什么这两个select语句没有给出相同的答案_Oracle_Oracle11g - Fatal编程技术网

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

Oracle 为什么这两个select语句没有给出相同的答案,oracle,oracle11g,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); 在第一个例子中,我选

我正在使用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);
在第一个例子中,我选择了所有不存在的东西

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

注意:存在已更改为不存在,并且!=改为=


看起来一样,对吗?但是它们给出了完全不同的答案

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

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

select *
from tableA 
where exists (select * from tableB where tableA.ID != tableB.ID);
只有当一行与TableB中ID值为非空的所有行相同时,该行才会失败匹配。因此,如果TableB中至少有两行ID不同,则tableA中的所有行都将返回

本声明:

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

表示表B中没有与表A中的id匹配的id。99%的情况下,这就是您想要的。

+1,但第一种情况下可能有多行:第二行的ID字段中必须包含null。