oracle左外部联接不显示右空值

oracle左外部联接不显示右空值,oracle,join,Oracle,Join,我在oracle中创建一个查询时遇到了一个问题,该查询似乎不想加入缺少的值 我的表格如下: table myTable(refnum, contid, type) values are: 1, 10, 90000 2, 20, 90000 3, 30, 90000 4, 20, 10000 5, 30, 10000 6, 10, 20000 7, 20, 20000 8, 30, 20000 我所关注的领域细分如下: select a.refnum from myTable a where

我在oracle中创建一个查询时遇到了一个问题,该查询似乎不想加入缺少的值

我的表格如下:

table myTable(refnum, contid, type)

values are:
1, 10, 90000
2, 20, 90000
3, 30, 90000
4, 20, 10000
5, 30, 10000
6, 10, 20000
7, 20, 20000
8, 30, 20000
我所关注的领域细分如下:

select a.refnum from myTable a where type = 90000
select b.refnum from myTable b where type = 10000 and contid in (select contid from myTable where type = 90000)
select c.refnum from myTable c where type = 20000 and contid in (select contid from myTable where type = 90000)
a.refnum, b.refnum, c.refnum
我要查询的结果如下:

select a.refnum from myTable a where type = 90000
select b.refnum from myTable b where type = 10000 and contid in (select contid from myTable where type = 90000)
select c.refnum from myTable c where type = 20000 and contid in (select contid from myTable where type = 90000)
a.refnum, b.refnum, c.refnum
我认为这会奏效:

select a.refnum, b.refnum, c.refnum
from myTable a 
left outer join myTable b on (a.contid = b.contid) 
left outer join myTable c on (a.contid = c.contid) 
where a.id_tp_cd = 90000
and b.id_tp_cd = 10000
and c.id_tp_cd = 20000
因此,值应为:

1, null, 6
2, 4, 7
3, 5, 8
但它唯一的回报是:

2, 4, 7
3, 5, 8
我认为左连接将显示左侧的所有值,并为右侧创建null


帮助:(

您的说法是正确的,左侧联接将在不匹配的情况下为右侧返回空值,但在您将此限制添加到where子句时,您不允许返回这些空值:

and b.id_tp_cd = 10000
and c.id_tp_cd = 20000
您应该能够将它们放在join的'on'子句中,这样只返回右侧的相关行

select a.refnum, b.refnum, c.refnum
from myTable a 
left outer join myTable b on (a.contid = b.contid and b.id_tp_cd = 10000) 
left outer join myTable c on (a.contid = c.contid and c.id_tp_cd = 20000) 
where a.id_tp_cd = 90000

或者使用Oracle语法而不是ansi

select a.refnum, b.refnum, c.refnum
from myTable a, mytable b, mytable c
where a.contid=b.contid(+)
and a.contid=c.contid(+)
and a.type = 90000
and b.type(+) = 10000
and c.type(+) = 20000;


REFNUM     REFNUM     REFNUM
---------- ---------- ----------
     1                     6
     2          4          7
     3          5          8