SQL “Join” on null values

前端 未结 14 1335
旧巷少年郎
旧巷少年郎 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 14:46

    In SQL Server I have used:

    WHERE (a.col = b.col OR COALESCE(a.col, b.col) IS NULL)
    

    Obviously not efficient, because of the OR, but unless there's a reserved value you can map NULLs to on both sides without ambiguity or folding that's about the best you can do (and if there was, why was NULL even allowed in your design...)

提交回复
热议问题