java - jOOQ - 用于插入的多字段

标签 java sql jooq

我想表达以下INSERT声明:

context.insertInto(TABLE A)
   .set(<FIELD A, FIELD B>, context.select(FIELD A, FIELD B).from(B).where(...))
   .set(... other field of table A ...)
   .set(... other field of table A ...)
   .set(... other field of table A ...)
   .returning()
   .fetch()

子选择返回包含两列(FIELD AFIELD B)的一行,需要将其插入到目标TABLE A 中.原因是<FIELD A, FIELD B>TABLE B 的主键. TABLE A指的是 TABLE B (外键)。

这可能吗?

最佳答案

我不确定首先是否可以通过 INSERT .. VALUES 使用任何 SQL 方言,但是您当然可以使用 INSERT ..选择:

INSERT INTO a (a, b, x, y, z)
SELECT a, b, ... other value ..., ... other value ..., ... other value ...
FROM b
WHERE ...
RETURNING *;

或者使用 jOOQ

context.insertInto(TABLE A, ... columns ...)
       .select(
           select(
               FIELD A, 
               FIELD B,
               ... other field of table A ...,
               ... other field of table A ...,
               ... other field of table A ...)
          .from(B)
          .where(...))
       )
       .returning()
       .fetch()

关于java - jOOQ - 用于插入的多字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31959180/

相关文章:

java - 为什么我不能在子类中分配变量?

javascript - 如何在jsp端显示类似 "Please wait"的加载消息?

java - Spring Security基于XML的配置: java. lang.IllegalArgumentException:没有为id "null"映射的PasswordEncoder

c# - 将比较运算符作为参数传递给 SQL 查询

java - 使用证书 .p12 签署包 .deb

SQL查询: pivot but with different count or rows

sql - 跟踪 Rails 3 SQL 查询

java - 使用 postgres 和 jOOQ 的全文搜索和变量绑定(bind)不起作用

java - 如何在 Jooq 中为例程添加 OrderBy?

postgresql - 使用 jOOQ 从 Postgres 数组中删除多个元素