JOOQ插入具有指定列的select语法中

pay*_*liu 2 java sql select insert jooq

JOOQ可以对指定的列执行“插入选择”语法吗?我进行了几次尝试。

表格:

table1(预期插入)

id column1 column2 column3 ...时间戳
1 John Leo null 2012/1/28 23:32:23(预期插入)

表2

id col1 col2 col3  
101约翰xxx xxx(来自table2.col1)
102 xxx xxx xxx xxx

表3


id col1 col2 col3  
101 xxx狮子座xxx(来自table3.col2)
102 xxx xxx xxx xxx
id   column1   column2    column3 ...  timestamp
1    John      Leo        null         2012/1/28 23:32:23    (Expected insert)

JOOQ代码:

id   col1   col2   col3  
101  John   xxx    xxx     (from table2.col1)
102  xxx    xxx    xxx

JOOQ显示错误消息:

The number of values must match the number of fields
Run Code Online (Sandbox Code Playgroud)

任何人都知道我遇到了什么问题以及如何解决我的JOOQ代码。谢谢,付钱。

参考: 示例:INSERT SELECT语法支持

Luk*_*der 5

您使用的是INSERT .. VALUES语法,而不是INSERT .. SELECT语法。您的子查询提供了值,column1但您没有为提供任何值column2。手册“示例:INSERT SELECT语法支持”中进一步介绍了您要执行的操作。在您的情况下,这将显示为(jOOQ 2.x语法,在jOOQ 3.x中不再可用):

create.insertInto(table1, 
create.select( table2.col1, table3.col2 )
      .from(table2)
      .join(table3)
      .on( table12.id.equal(table3.id) )
      .where( table2.id.equal(101) ))
      .execute(); //.getSQL();
Run Code Online (Sandbox Code Playgroud)

或使用自定义投影(jOOQ 3.x语法):

create.insertInto(table1, column1, column2)
      .select(create.select(...))
      .execute();
Run Code Online (Sandbox Code Playgroud)