我有一个问题
select c from DTO c where trafficLight = ?1当交通信号灯为红色或绿色时,它返回预期的行,但当交通信号灯为空时,它不返回任何内容。我知道对null的检查是"is null",但是在这里最好的方法是什么?谢谢。
发布于 2017-12-13 16:36:10
我希望表达式“
trafficLight = ?1”计算所有的情况
“我恐怕要倒霉了”
NULL表示没有任何值,因此根据定义,如果对NULL使用等于运算符,它将永远不会返回true:
where NULL = NULL -- is never true因为根本没有价值可以比较,所以根本没有办法知道它们是否相等,所以“不相等”也不起作用。我确实理解这很难理解,但“就是这样”,尽你所能尝试NULL = NULL永远不会是真的。然而,这将适用于所有情况。
where (trafficLight = ?1 and ?1 IS NULL)
where (trafficLight = 'red' or 'red' IS NULL) -- true when trafficLight = 'red'
where (trafficLight = 'amber' or 'amber' IS NULL) -- true when trafficLight = 'amber'
where (trafficLight = 'green' or 'green' IS NULL) -- true when trafficLight = 'green'
where (trafficLight = NULL or NULL IS NULL) -- true for allhttps://stackoverflow.com/questions/47788241
复制相似问题