jOOQ - 用于插入的多字段

时间:2015-08-12 07:52:37

标签: 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(外键)。

这可能吗?

1 个答案:

答案 0 :(得分:1)

我不确定首先是通过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()
相关问题