有没有更简单的实现方式
select (case when ((a is null) or (b is null)) then null else (case when
(a = b) then true else false end) end) from ...
在 PostgreSQL 或其他主要 RDBMS 中?
最佳答案
我认为最简单的是:
select (a = b)
尽管这不完全符合您的逻辑,因为如果 a
或 b
为 NULL
,则返回 NULL
.它可能已经足够好了。
如果您希望它返回 false
如果 a
或 b
是 NULL
但不是两者,则:
select (a = b) and nullif(coalesce(a, b) is null, true)
编辑:
因为您询问的是其他数据库,所以表达逻辑的一种方式是:
(case when a = b then 1 when a <> b then 0 end) as flag
如果其中一个为 NULL
,则返回 NULL
。
关于sql - SQL SELECT 中的 bool 逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57126784/