简体   繁体   中英

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:-

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:

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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