将 UNNEST 与 jOOQ 结合使用
Using UNNEST with jOOQ
我正在使用 PostgreSQL 9.4、Spring Boot 1.3.2 和 jOOQ 3.7。我想 "jOOQify" 以下查询
SELECT id FROM users WHERE username IN (SELECT * FROM UNNEST(?))
这样我就可以 运行 通过传递列表或数组(在本例中为 List<String>
)来 Spring 的 JdbcTemplate
。我试过了
DSLContext.select(
USERS.ID
)
.from(
USERS
)
.where(
USERS.USERNAME.in(...)
)
但我不知道要在 ...
中放置什么。我试过类似
DSLContext.select(field("*")).from(unnest(myList))
但不幸的是编译器不喜欢那样。有什么简单的方法可以实现吗?
编译器不喜欢您的尝试,因为 USERS.USERNAME
是 Field<String>
,因此 in()
方法需要 Select<? extends Record1<String>>
.
您可以这样修复它:
select(field("*", String.class)).from(unnest(myList))
或者,也许更好一点:
select(field(name("v"), String.class)).from(unnest(myList).as("t", "v"))
我正在使用 PostgreSQL 9.4、Spring Boot 1.3.2 和 jOOQ 3.7。我想 "jOOQify" 以下查询
SELECT id FROM users WHERE username IN (SELECT * FROM UNNEST(?))
这样我就可以 运行 通过传递列表或数组(在本例中为 List<String>
)来 Spring 的 JdbcTemplate
。我试过了
DSLContext.select(
USERS.ID
)
.from(
USERS
)
.where(
USERS.USERNAME.in(...)
)
但我不知道要在 ...
中放置什么。我试过类似
DSLContext.select(field("*")).from(unnest(myList))
但不幸的是编译器不喜欢那样。有什么简单的方法可以实现吗?
编译器不喜欢您的尝试,因为 USERS.USERNAME
是 Field<String>
,因此 in()
方法需要 Select<? extends Record1<String>>
.
您可以这样修复它:
select(field("*", String.class)).from(unnest(myList))
或者,也许更好一点:
select(field(name("v"), String.class)).from(unnest(myList).as("t", "v"))