现在我知道你不能直接将NULL与任何东西进行比较(因为null是未知的)所以我将如何实现以下目标:
select *
from Material as m
where MtrlCode = 826 and
Exposlimit <> 'compareMe'
Exposlimit可能为NULL或者可能不是。 'compareMe'也可能是NULL。
因此我如何比较两者?双方可以是文本也可以是NULL。
答案 0 :(得分:5)
select *
from Material as m
where MtrlCode = 826
and (Exposlimit <> 'compareMe'
or (Exposlimit is null and compareme is not null)
or (Exposlimi is not null and compareme is null))
答案 1 :(得分:2)
在这种情况下使用IFNULL功能。
即
哪里有IFNULL(FieldA,'MagicConstant')= IFNULL(FieldB,'MagicConstant')
答案 2 :(得分:1)
select *
from Material as m
where (MtrlCode = 826 or MtrlCode IS NULL) and
(Exposlimit <> 'compareMe' or Exposlimit IS NULL)
答案 3 :(得分:0)
你试过这个吗?
select *
from Material as m
where MtrlCode = 826 and
Exposlimit IS NOT NULL AND 'compareMe' IS NOT NULL AND Exposlimit <> 'compareMe'
答案 4 :(得分:0)
考虑找到平等更容易:
(Column = @Value or (Column is null and @Value is null))
当两个值相等时,结果为true。
理想情况下,我们可以否定这个语句来查找不等式,但SQL的三态逻辑打破了这个想法,如NOT(UNKNOWN) = UNKNOWN
--DO NOT USE, broken
NOT (Column = @Value or (Column is null and @Value is null))
因此,如果我们仅检查TRUE
值并取消它,我们仍然会以可读的操作结束。
CASE WHEN Column is null and @Value is null or Column = @Value THEN 1 ELSE 0 END = 0