当前位置:  开发笔记 > 编程语言 > 正文

oracle左外连接没有显示正确的空值

如何解决《oracle左外连接没有显示正确的空值》经验,为你挑选了1个好方法。

我在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, 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.

救命 :(



1> Tom Haigh..:

你说左边连接将返回没有匹配的右边的空值是正确的,但是当你将这个限制添加到where子句时,你不允许返回这些空值:

and b.id_tp_cd = 10000
and c.id_tp_cd = 20000

您应该能够将这些放在连接的"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

推荐阅读
Chloemw
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有