如何将 SQL 中的值相等与 null 进行比较?
对于熟悉 C# 的人,以下是比较可空值的结果:
null == null : true
null == john : false
null == paul : false
john == null : false
john == john : true
john == paul : false
paul == null : false
paul == john : false
paul == paul : true
我在 SQL 中找到的最简单的解决方案是将可为空的字段合并为一些标记值(例如“scoobydoo”),然后比较它们
coalesce(A, 'scoobydoo') = coalesce(B, 'scoobydoo')
但是,如果有人使用哨兵值,这很简单,如果 A 恰好为 NULL 而 B 是“scoobydoo”,那么上面的表达式将产生 true
这正是我询问上述代码逻辑的目的(T-SQL UPDATE 触发器):
-- detect if the value changes
if (select invoice_date from inserted) <>
(select invoice_date from deleted) begin
-- do something to summary tables here
end
如何在 SQL 中使用类似 C# 的行为进行相等比较?
[编辑:在这里找到答案]
测试了代码(Postgres 不错的布尔支持,FTW!):
select
A, B,
A = B,
A IS NOT DISTINCT FROM B, -- "logically" same as above
A <> B,
A IS DISTINCT FROM B -- "logically" same as above
from(
values
(null, null),
(null, 'john'),
(null, 'paul'),
('john', null),
('john', 'john'),
('john', 'paul'),
('paul', null),
('paul', 'john'),
('paul', 'paul')) as x(A,B)
[编辑:测试了乔恩的代码,他对等式的回答是半工作的(只是将 null 视为假),但他对不等式的回答会爆炸]
测试了代码(Postgres 不错的布尔支持,FTW!):
select
A, B,
A = B,
A IS NOT DISTINCT FROM B, -- "logically" same as above
coalesce( (A = B) or (A is null and B is null), false ),
-- tested Jon's code for ==, semi-work, coalesced to make it true/false only
A <> B,
A IS DISTINCT FROM B, -- "logically" same as above
(A <> B) and (A is not null or B is not null)
-- tested Jon's code for !=, bombs out
from(
values
(null, null),
(null, 'john'),
(null, 'paul'),
('john', null),
('john', 'john'),
('john', 'paul'),
('paul', null),
('paul', 'john'),
('paul', 'paul')) as x(A,B)
[编辑:发布了另一个与此相关的问题]
[编辑:基于 Jon 对不等式比较的非工作语义的调查发布的结果]
select
A, B,
A = B,
A IS NOT DISTINCT FROM B, -- "logically" same as above
(A = B) or (A is null and B is null),
-- tested Jon's code for ==
A <> B,
A IS DISTINCT FROM B -- "logically" same as above,
(A <> B) and (A is not null or B is not null)
-- tested Jon's code for !=, bombs out
from(
values
(null, null),
(null, 'john'),
(null, 'paul'),
('john', null),
('john', 'john'),
('john', 'paul'),
('paul', null),
('paul', 'john'),
('paul', 'paul')) as x(A,B)
a | b | ?column? | ?column? | ?column? | ?column? | ?column? | ?column?
------+------+----------+----------+----------+----------+----------+----------
null | null | null | t | t | null | f | f
null | john | null | f | null | null | t | null
null | paul | null | f | null | null | t | null
john | null | null | f | null | null | t | null
john | john | t | t | t | f | f | f
john | paul | f | f | f | t | t | t
paul | null | null | f | null | null | t | null
paul | john | f | f | f | t | t | t
paul | paul | t | t | t | f | f | f
(9 rows)
不平等的非工作语义促使我发布另一个问题:-)
[编辑:测试乔恩的新答案]
select
A, B,
A = B as e,
A IS NOT DISTINCT FROM B AS e_works, -- "logically" same as above
(A = B) or (A is null and B is null) AS e_semi_work, -- tested Jon's code for ==, works if we treat null as false
A <> B as ie,
A IS DISTINCT FROM B as ie_works, -- "logically" same as above,
(A <> B) and (A is not null or B is not null) as ie_not_work, -- tested Jon's code for !=, bombs out
(A <> B) or ((A is null or B is null) and (A is not null or B is not null)) as ie_semi_works, -- this works(well it is, if you treat null as false),
not ((A = B) or (A is null and B is null)) as ie_not_work2 -- this doesn't work
from(
values
(null, null),
(null, 'john'),
(null, 'paul'),
('john', null),
('john', 'john'),
('john', 'paul'),
('paul', null),
('paul', 'john'),
('paul', 'paul')) as x(A,B)
结果:
a | b | e | e_works | e_semi_work | ie | ie_works | ie_not_work | ie_semi_works | ie_not_work2
------+------+------+---------+-------------+------+----------+-------------+---------------+--------------
null | null | null | t | t | null | f | f | null | f
null | john | null | f | null | null | t | null | t | null
null | paul | null | f | null | null | t | null | t | null
john | null | null | f | null | null | t | null | t | null
john | john | t | t | t | f | f | f | f | f
john | paul | f | f | f | t | t | t | t | t
paul | null | null | f | null | null | t | null | t | null
paul | john | f | f | f | t | t | t | t | t
paul | paul | t | t | t | f | f | f | f | f
(9 rows)