首页 > 解决方案 > Scala Doobie 查询硬编码值

问题描述

在 Doobie 中编写以下查询时:

(SELECT id FROM (VALUES(?),(?),(?)) table(id))
UNION
SELECT id FROM table

我有数据列表,例如 List(1,2,3,4) ,它是可变大小的。如何使用 Doobie 将值列表插入 SQL VALUES CLAUSE?

标签: sqlscalarelational-databasedoobie

解决方案


您好,您可以参考以下来自 doobie 官方社区页面的参考资料:

Consider a below table DDL:     
CREATE TABLE country (
          code       character(3)  NOT NULL,
          name       text          NOT NULL,
          population integer       NOT NULL,
          gnp        numeric(10,2)
          -- more columns, but we won't use them here
    )

SQL 使用 doobie 语法,其中代码是不同大小的列表:

sql"""
    select code, name, population, gnp 
    from country
    where code in (${codes : codes.type})
""".query[Country]

推荐阅读