MySQL mystery: Null value is not different from non-null string

前端 未结 5 700
伪装坚强ぢ
伪装坚强ぢ 2021-01-20 16:25

Why is this query returning 0 rows?

select t.f1, t.f2
from (select null f1, \'a\' f2 from dual) t
where t.f1<>t.f2;

This is a distill

5条回答
  •  耶瑟儿~
    2021-01-20 16:50

    NULL value is nothing, it can't be equal or not equal to something. If you want to check if your value is null - use "IS NULL" statement:

    select t.f1, t.f2
    from (select null f1, 'a' f2 from dual) t
    where t.f1 IS NULL
    

    If you want to check if your values are equal or not equal - you can use COALESCE function on nullable columns:

    select t.f1, t.f2
    from (select null f1, 'a' f2 from dual) t
    where COALESCE(t.f1, '')<>COALESCE(t.f2, '');
    

提交回复
热议问题