如何在PostgreSQL的WHERE中使用CASE条件?

问题描述 投票:0回答:1

我有一个带有自定义查询的 Spring boot 应用程序。 我需要根据条件查找所有数据库行 - 如果

myField
为 true,则数据库中的字段必须位于列表中(此处为
values
),或者为 null。 我可以用类似的东西吗

select ...
from ...
where ...
and (
           case
             when :myFlag then value in (:values)
             else value in (:values) or value is null
           end
        )

例如,如果我有行

id | value
1  | 1
2  | null
3  | 3

和我的

values = [1,3]
myFlag = true
,那么查询必须返回 id = 1, 2, 3 的行。

sql database postgresql
1个回答
0
投票

您不需要这里的案例,

select ...
from ...
where ...
and (
  value in (:values)
  OR (:myFlag AND value IS NULL)
)
© www.soinside.com 2019 - 2024. All rights reserved.