【问题标题】:Strange behaviour SQL on comparing null values比较空值时的奇怪行为 SQL
【发布时间】:2018-09-26 07:58:42
【问题描述】:

我正在比较两个表格并得到一个奇怪的结果。我开始比较两个通常为空的列。声明 null = null 总是返回 false。我发现这些列的 make is NULL 有效。结果显示很多双行,看不懂。

所以基本上这行data.data_1

column1     column2     column3     column4
apple       tree        NULL        NULL        

还有这一行data.data_2

column1     column2     column3     column4
apple       tree        NULL        NULL        

在以下查询后返回

select
    br.column1, br.column2, br.column3, br.column4,  
    af.column1, af.column2
from
    data.data_1 as br
    left join data.data_2 as af on 
        (br.column1 = af.column1 and
        br.column2 = af.column2 and
        br.column3 IS NULL and af.column3 IS NULL and 
        br.column4 IS NULL and af.column4 IS NULL)

column1     column2     column3     column4     af.column1     af.column2     
apple       tree        NULL        NULL         apple          tree 
apple       tree        NULL        NULL         apple          tree 
apple       tree        NULL        NULL         apple          tree      
apple       tree        NULL        NULL         apple          tree      
apple       tree        NULL        NULL         apple          tree      
apple       tree        NULL        NULL         apple          tree      
apple       tree        NULL        NULL         apple          tree      
apple       tree        NULL        NULL         apple          tree      
apple       tree        NULL        NULL         apple          tree      
apple       tree        NULL        NULL         apple          tree      
apple       tree        NULL        NULL         apple          tree      
apple       tree        NULL        NULL         apple          tree      
apple       tree        NULL        NULL         apple          tree      
apple       tree        NULL        NULL         apple          tree      
apple       tree        NULL        NULL         apple          tree    

而不是我希望它返回的内容是:

column1     column2     column3     column4     af.column1     af.column2     
apple       tree        NULL        NULL         apple          tree 

我可以在它周围添加一个独特的功能,但我觉得这将是一个不必要的额外操作。

【问题讨论】:

  • NULL 可能会造成混淆。这篇文章有帮助吗? xaprb.com/blog/2006/05/18/…
  • 或许你想比较一下AND ((br.column3 IS NULL AND af.column3 IS NULL) OR (br.column3 = af.column3))
  • 你说double values是什么意思?
  • null = null 不返回 false。它返回未知。记住这一点很重要,因为not (null = null)也是未知(而且绝对不是真的)
  • 结果与样本数据不符。

标签: sql postgresql null


【解决方案1】:

demo: db<>fiddle

SELECT *
FROM br
LEFT JOIN af
ON 
    br.col1 = af.col1 
AND br.col2 = af.col2 
AND COALESCE(br.col3, af.col3, br.col4, af.col4) IS NULL

COALESCE() IS NULL 确保此函数中的所有元素(所有列)都是NULL

【讨论】:

  • 谢谢;现在就试试这个,然后回到这里
  • @Zuenie 添加小提琴
  • 这有帮助。完美
  • 但这与原始查询基本相同(具有 4 个 IS NULL 的查询)...
【解决方案2】:

如果你想比较两个可以为空的列并且想考虑 NULL = NULL 那么使用IS NOT DISTINCT FROM:

SELECT CASE WHEN NULL = NULL                    THEN 'Equal' ELSE 'IDK' END       -- IDK
SELECT CASE WHEN NULL IS NOT DISTINCT FROM NULL THEN 'Equal' ELSE 'Not Equal' END -- Equal

只需将此条件插入您的原始查询中:

select
    br.column1, br.column2, br.column3, br.column4,
    af.column1, af.column2
from
    data.data_1 as br
    left join data.data_2 as af on (
        br.column1 = af.column1 and
        br.column2 = af.column2 and
        br.column3 IS NOT DISTINCT FROM af.column3 and
        br.column4 IS NOT DISTINCT FROM af.column4
    )

【讨论】:

    【解决方案3】:

    left join 使查询列出来自data_1 的所有行,无论data_2 中是否有匹配的行。

    此外,只有当 column3column4 在两个表中都是 NULL 时才加入 - 并列出这些 NULL 值。委婉地说,这有点令人困惑。

    如果你写下你想要达到的目标,你的问题会更容易回答。

    【讨论】:

    • 这是我所期望的。但它没有。它返回比输入表更多的行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    相关资源
    最近更新 更多