SQL “Join” on null values

前端 未结 14 1286
旧巷少年郎
旧巷少年郎 2020-12-13 14:04

For reasons beyond my control, I need to join two tables and I need null values to match. The best option I could think of was to spit out a UUID and use that as my comparis

相关标签:
14条回答
  • 2020-12-13 15:00

    Why not something like that :

    SELECT * FROM T1 JOIN T2 ON nvl(T1.SOMECOL,'null') = nvl(T2.SOMECOL,'null')

    I don't know why you are using the UUID. You could use any string not present in the columns, like the string "null", for example, for lower memory footprint. And the solution using nvl is much faster than the solution using or ... is null proposed by Eric Petroelje, for example.

    0 讨论(0)
  • 2020-12-13 15:03

    You can't do any better, but the JOIN you have will not do an actual "JOIN" in any way (there won't be any correlation between T1.SOMECOL and T2.SOMECOL other than they both have a NULL value for that column). Basically that means that you won't be able to use a JOIN on NULLs to see if rows match.

    NULL is never equal to another NULL. How can something of unknown value be equal to something else of unknown value?

    0 讨论(0)
提交回复
热议问题