【问题标题】:Not in condition does not consider null in Oracle不在条件下不考虑 Oracle 中的 null
【发布时间】:2016-02-01 10:58:27
【问题描述】:

我的结果集如下

reg_no    name    grade
--------- ------- ------
1112      Muthu   A
1113      Vikki   B
1114      Murali  C
1115      Shankar null

我已经用 not in 检查了条件

select * from grades where grade not in ('B','C')

但是在执行查询时,我只有一条记录是 A 级。第4行我没看。我坚持这一点。如何检查。

【问题讨论】:

    标签: oracle null notin


    【解决方案1】:

    检查 null 存在的唯一一种正确方法是 IS NULLIS NOT NULL

    select * from grades where grade not in ('B','C') or grade is null
    

    【讨论】:

    • 谢谢 piotr。但我对此表示怀疑。为什么我们需要检查null并且它不返回结果
    • 您不能将 null 与其他值进行比较,它会产生 null 作为结果。 en.wikipedia.org/wiki/Null_%28SQL%29
    • 谢谢皮奥特。很有用
    【解决方案2】:

    这样检查

    select * from grades where nvl(grade,'?') not in ('B','C');
    

    但是这个适当的不能在 grafes 上使用索引,如果存在的话。

    【讨论】:

      猜你喜欢
      • 2019-09-28
      • 2016-11-28
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-21
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多