为什么查询在oracle中不能按预期工作?它返回0行,但应返回2行

为什么查询在oracle中不能按预期工作?它返回0行,但应返回2行,oracle,Oracle,我有三个表A、B和C。表B和C中的id1列是表A列id的外键 请看下面的结构 表A: id value 1 1 2 2 3 3 表B: id value id1 1 2018-04-21-00001 1 2 2018-04-21-00003 1 3 2018-04-21-00009 1 4 2018-04-21-00007 1 5 2018-04-21-00008 1 表C: id2 value2 id1 1 2

我有三个表A、B和C。表B和C中的id1列是表A列id的外键

请看下面的结构

表A:

id value
1  1
2  2
3  3
表B:

id value            id1
1  2018-04-21-00001   1
2  2018-04-21-00003   1
3  2018-04-21-00009   1
4  2018-04-21-00007   1
5  2018-04-21-00008   1
表C:

id2 value2           id1
1  2018-04-21-00001   1
2                     1
3  2018-04-21-00002   1
4                     1
5  2018-04-21-00004   1
6                     1
7  2018-04-21-00006   2
查询:

select * from B b, C c  
where c.id1 = b.id1 and c.id1 = 1 and 
c.value2 is not null and c.value2 <> b.value 

我假设它应该返回2行,但它没有按预期工作。它正在返回空行。你能帮我解答一下这个问题吗。查询在oracle中。

在初始联接条件c.id1=b.id1时,您将获得id1级别的笛卡尔积。对于您的样本数据,将是5*6或30条结果记录

+----+------------------+-----+----+------------------+-----+
| id |      value       | id1 | id |      value2      | id1 |
+----+------------------+-----+----+------------------+-----+
|  1 | 2018-04-21-00001 |   1 |  1 | 2018-04-21-00001 |   1 |
|  1 | 2018-04-21-00001 |   1 |  2 | (null)           |   1 |
|  1 | 2018-04-21-00001 |   1 |  3 | 2018-04-21-00002 |   1 |
|  1 | 2018-04-21-00001 |   1 |  4 | (null)           |   1 |
|  1 | 2018-04-21-00001 |   1 |  5 | 2018-04-21-00004 |   1 |
|  1 | 2018-04-21-00001 |   1 |  6 | (null)           |   1 |
|  2 | 2018-04-21-00003 |   1 |  1 | 2018-04-21-00001 |   1 |
|  2 | 2018-04-21-00003 |   1 |  2 | (null)           |   1 |
|  2 | 2018-04-21-00003 |   1 |  3 | 2018-04-21-00002 |   1 |
|  2 | 2018-04-21-00003 |   1 |  4 | (null)           |   1 |
|  2 | 2018-04-21-00003 |   1 |  5 | 2018-04-21-00004 |   1 |
|  2 | 2018-04-21-00003 |   1 |  6 | (null)           |   1 |
|  3 | 2018-04-21-00009 |   1 |  1 | 2018-04-21-00001 |   1 |
|  3 | 2018-04-21-00009 |   1 |  2 | (null)           |   1 |
|  3 | 2018-04-21-00009 |   1 |  3 | 2018-04-21-00002 |   1 |
|  3 | 2018-04-21-00009 |   1 |  4 | (null)           |   1 |
|  3 | 2018-04-21-00009 |   1 |  5 | 2018-04-21-00004 |   1 |
|  3 | 2018-04-21-00009 |   1 |  6 | (null)           |   1 |
|  4 | 2018-04-21-00007 |   1 |  1 | 2018-04-21-00001 |   1 |
|  4 | 2018-04-21-00007 |   1 |  2 | (null)           |   1 |
|  4 | 2018-04-21-00007 |   1 |  3 | 2018-04-21-00002 |   1 |
|  4 | 2018-04-21-00007 |   1 |  4 | (null)           |   1 |
|  4 | 2018-04-21-00007 |   1 |  5 | 2018-04-21-00004 |   1 |
|  4 | 2018-04-21-00007 |   1 |  6 | (null)           |   1 |
|  5 | 2018-04-21-00008 |   1 |  1 | 2018-04-21-00001 |   1 |
|  5 | 2018-04-21-00008 |   1 |  2 | (null)           |   1 |
|  5 | 2018-04-21-00008 |   1 |  3 | 2018-04-21-00002 |   1 |
|  5 | 2018-04-21-00008 |   1 |  4 | (null)           |   1 |
|  5 | 2018-04-21-00008 |   1 |  5 | 2018-04-21-00004 |   1 |
|  5 | 2018-04-21-00008 |   1 |  6 | (null)           |   1 |
+----+------------------+-----+----+------------------+-----+
从该结果集中,您可以说c.value2不是null,将其减少到15条记录

+----+------------------+-----+----+------------------+-----+
| id |      value       | id1 | id |      value2      | id1 |
+----+------------------+-----+----+------------------+-----+
|  1 | 2018-04-21-00001 |   1 |  1 | 2018-04-21-00001 |   1 |
|  2 | 2018-04-21-00003 |   1 |  1 | 2018-04-21-00001 |   1 |
|  3 | 2018-04-21-00009 |   1 |  1 | 2018-04-21-00001 |   1 |
|  4 | 2018-04-21-00007 |   1 |  1 | 2018-04-21-00001 |   1 |
|  5 | 2018-04-21-00008 |   1 |  1 | 2018-04-21-00001 |   1 |
|  1 | 2018-04-21-00001 |   1 |  3 | 2018-04-21-00002 |   1 |
|  2 | 2018-04-21-00003 |   1 |  3 | 2018-04-21-00002 |   1 |
|  3 | 2018-04-21-00009 |   1 |  3 | 2018-04-21-00002 |   1 |
|  4 | 2018-04-21-00007 |   1 |  3 | 2018-04-21-00002 |   1 |
|  5 | 2018-04-21-00008 |   1 |  3 | 2018-04-21-00002 |   1 |
|  1 | 2018-04-21-00001 |   1 |  5 | 2018-04-21-00004 |   1 |
|  2 | 2018-04-21-00003 |   1 |  5 | 2018-04-21-00004 |   1 |
|  3 | 2018-04-21-00009 |   1 |  5 | 2018-04-21-00004 |   1 |
|  4 | 2018-04-21-00007 |   1 |  5 | 2018-04-21-00004 |   1 |
|  5 | 2018-04-21-00008 |   1 |  5 | 2018-04-21-00004 |   1 |
+----+------------------+-----+----+------------------+-----+
然后你说c.value2 b.value,这将给你留下14条记录,因为只有2018-04-21-00001会被删除的记录

+----+------------------+-----+----+------------------+-----+
| id |      value       | id1 | id |      value2      | id1 |
+----+------------------+-----+----+------------------+-----+
|  2 | 2018-04-21-00003 |   1 |  1 | 2018-04-21-00001 |   1 |
|  3 | 2018-04-21-00009 |   1 |  1 | 2018-04-21-00001 |   1 |
|  4 | 2018-04-21-00007 |   1 |  1 | 2018-04-21-00001 |   1 |
|  5 | 2018-04-21-00008 |   1 |  1 | 2018-04-21-00001 |   1 |
|  1 | 2018-04-21-00001 |   1 |  3 | 2018-04-21-00002 |   1 |
|  2 | 2018-04-21-00003 |   1 |  3 | 2018-04-21-00002 |   1 |
|  3 | 2018-04-21-00009 |   1 |  3 | 2018-04-21-00002 |   1 |
|  4 | 2018-04-21-00007 |   1 |  3 | 2018-04-21-00002 |   1 |
|  5 | 2018-04-21-00008 |   1 |  3 | 2018-04-21-00002 |   1 |
|  1 | 2018-04-21-00001 |   1 |  5 | 2018-04-21-00004 |   1 |
|  2 | 2018-04-21-00003 |   1 |  5 | 2018-04-21-00004 |   1 |
|  3 | 2018-04-21-00009 |   1 |  5 | 2018-04-21-00004 |   1 |
|  4 | 2018-04-21-00007 |   1 |  5 | 2018-04-21-00004 |   1 |
|  5 | 2018-04-21-00008 |   1 |  5 | 2018-04-21-00004 |   1 |
+----+------------------+-----+----+------------------+-----+

如果你像我在这里做的那样一步一步地构建你的WHERE,这将更加明显。 相反,对于id1=1,您只需要c中不在b中的记录。因此:


桌子A是用来做什么的

如果连接正确,查询将返回2个所需的行:

SQL> with b (id, value, id1) as
  2    (select 1, '00001', 1 from dual union all
  3     select 2, '00003', 1 from dual union all
  4     select 3, '00009', 1 from dual union all
  5     select 4, '00007', 1 from dual union all
  6     select 5, '00008', 1 from dual
  7    ),
  8  c (id2, value2, id1) as
  9    (select 1, '00001', 1 from dual union all
 10     select 2, null   , 1 from dual union all 
 11     select 3, '00002', 1 from dual union all
 12     select 4, null   , 1 from dual union all 
 13     select 5, '00004', 1 from dual union all
 14     select 6, null   , 1 from dual union all 
 15     select 7, '00006', 2 from dual           
 16    )
 17  select *
 18  from b join c on b.id = c.id2
 19  where c.id1 = 1
 20    and c.value2 is not null
 21    and c.value2 <> b.value
 22  ;

        ID VALUE        ID1        ID2 VALUE        ID1
---------- ----- ---------- ---------- ----- ----------
         3 00009          1          3 00002          1
         5 00008          1          5 00004          1

SQL>

如果我想从表C中获取不在表B中的记录,如“2018-04-21-00002”,我该如何获取它?我的问题是什么@邪恶
SQL> with b (id, value, id1) as
  2    (select 1, '00001', 1 from dual union all
  3     select 2, '00003', 1 from dual union all
  4     select 3, '00009', 1 from dual union all
  5     select 4, '00007', 1 from dual union all
  6     select 5, '00008', 1 from dual
  7    ),
  8  c (id2, value2, id1) as
  9    (select 1, '00001', 1 from dual union all
 10     select 2, null   , 1 from dual union all 
 11     select 3, '00002', 1 from dual union all
 12     select 4, null   , 1 from dual union all 
 13     select 5, '00004', 1 from dual union all
 14     select 6, null   , 1 from dual union all 
 15     select 7, '00006', 2 from dual           
 16    )
 17  select *
 18  from b join c on b.id = c.id2
 19  where c.id1 = 1
 20    and c.value2 is not null
 21    and c.value2 <> b.value
 22  ;

        ID VALUE        ID1        ID2 VALUE        ID1
---------- ----- ---------- ---------- ----- ----------
         3 00009          1          3 00002          1
         5 00008          1          5 00004          1

SQL>