为了账号安全,请及时绑定邮箱和手机立即绑定

如何通过 postgresql 查询在字符串列表中搜索枚举?

如何通过 postgresql 查询在字符串列表中搜索枚举?

料青山看我应如是 2023-12-13 16:26:36
考虑一个 SQL 语句:select * from table where status in <statuses>其中 status 是枚举:CREATE TYPE statusType AS ENUM (  'enum1',  'enum2';在 Java 中,我有一个字符串表示形式的枚举列表:List<String> list = new ArrayList<>();list.add("enum1");list.add("enum2");然后,我尝试使用 SqlStatement 构建并执行查询:handle.createQuery("select * from table where status in <statuses>")                    .bindList("statuses", statuses)                    .map(Mapper.instance)                    .list());我不断收到以下错误:org.jdbi.v3.core.statement.UnableToExecuteStatementException: org.postgresql.util.PSQLException: ERROR: operator does not exist: status = character varying  Hint: No operator matches the given name and argument types. You might need to add explicit type casts.我尝试用 CAST(enum1 as statusType) 或 enum1::statusType 包装每个列表项,但没有成功。任何帮助表示赞赏。
查看完整描述

1 回答

?
拉莫斯之舞

TA贡献1820条经验 获得超10个赞

您可以将枚举转换为文本


handle.createQuery("select * from table where status::text in <statuses>")

                .bindList("statuses", statuses)

                .map(Mapper.instance)

                .list());


查看完整回答
反对 回复 2023-12-13
  • 1 回答
  • 0 关注
  • 52 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信