jOOQ - 用于插入的多字段

jOOQ - multi-field for insertion

我想表达以下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()

子selectreturns一行两列(FIELD AFIELD B)需要插入目标TABLE A。原因是<FIELD A, FIELD B>TABLE B的主键。 TABLE A 指的是 TABLE B(外键)。

这可能吗?

我不确定首先通过 INSERT .. VALUES 使用任何 SQL 方言是否可行,但您当然可以使用 INSERT .. SELECT 表达这种查询:

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()