简体   繁体   English

左外部联接具有空值

[英]left outer join with null values

**Table1**                  **Table2**    

ID       Values           ID         Values
1        100              1          10
2        200              2          20
3        300              3          30
4        400              4          40
null     2000             null       3000
5        500

o/p:- o / p:-

ID       Table1_Values  Table2_Values
1        100            10
2        200            20
3        300            30
4        400            40
5        500            null
null    2000            3000

Try this .. 尝试这个 ..

select t1.id,t1.values,t2.values from
table1 t1 
left outer join
table t2 on nvl(t1.id,0)=nvl(t2.id,0)

You can add a check to see if both values are NULL to the join condition: 您可以添加一个检查以查看连接条件两个值是否均为NULL

SELECT t1.ID,
       t1.VALUES AS Table1Values,
       t2.VALUES AS Table2Values
FROM   TABLE2 t1
       LEFT OUTER JOIN
       TABLE2 t2
       ON ( t1.ID = t2.ID OR ( t1.ID IS NULL AND t2.ID IS NULL ) )

There is an Oracle function Sys_Op_Map_NonNull that has been used for many versions as part of a materialised view refresh query for just this purpose. 为此,有一个Oracle函数Sys_Op_Map_NonNull已作为物化视图刷新查询的一部分用于许多版本。

https://oraclesponge.wordpress.com/2006/04/12/a-quick-materialized-view-performance-note/ https://oraclesponge.wordpress.com/2006/04/12/a-quick-materialized-view-performance-note/

It used to be entirely undocumented, but is now mentioned as a means of optimising fast refresh: http://docs.oracle.com/database/121/DWHSG/basicmv.htm 它曾经完全没有文档记录,但是现在作为优化快速刷新的一种方式而被提及: http : //docs.oracle.com/database/121/DWHSG/basicmv.htm

So you could: 因此,您可以:

select ...
from   t1 left outer join t2 on (sys_op_map_nonnull(t1.id) = sys_op_map_nonnull(t2.id))

If you were joining a small set of the table then function-based indexes would help, but I wouldn't bother if you're joining all rows. 如果要联接一小部分表,则基于函数的索引会有所帮助,但是如果要联接所有行,我就不会打扰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM