jOOQ – 用于插入的多字段

我想表达以下INSERT语句:

 context.insertInto(TABLE A) .set(, 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 。 其原因是TABLE B的主键。 TABLE A TABLE B (外键)。

这可能吗?

我不确定这是否可以通过任何SQL方言,通过INSERT .. VALUES ,但你当然可以使用INSERT .. SELECT来表达那种查询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()