简体   繁体   English

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

[英]JOOQ insert into select syntax with specified columns

Can JOOQ do 'insert into select' syntax for specified columns? JOOQ可以对指定的列执行“插入选择”语法吗? I run several different tries.. 我进行了几次尝试。

The Tables: 表格:

table1 (Expected insert) table1(预期插入)

id   column1   column2    column3 ...  timestamp
1    John      Leo        null         2012/1/28 23:32:23    (Expected insert)

table2 表2

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

table3 表3


id   col1   col2   col3  
101  xxx    Leo    xxx     (from table3.col2)
102  xxx    xxx    xxx
INSERT INTO table1 ( column1, column2 )
SELECT  table2.col1, table3.col2
FROM table2 join table3 t3 on table2.id = table3.id 
where table2.id = 101;

JOOQ code: JOOQ代码:

create.insertInto(table1, column1, column2 )
      .values( create.select( table2.col1, table3.col2 )
                     .from(table2)
                     .join(table3)
                     .on( table12.id.equal(table3.id) )
                     .where( table2.id.equal(101) ))
     .execute(); //.getSQL();

JOOQ show error message: JOOQ显示错误消息:

The number of values must match the number of fields

Anyone know what problem I make and how I can fix my JOOQ code. 任何人都知道我遇到了什么问题以及如何解决我的JOOQ代码。 thanks, Pay. 谢谢,付钱。

Reference: Example: INSERT SELECT syntax support 参考: 示例:INSERT SELECT语法支持

You're using the INSERT .. VALUES syntax, instead of the INSERT .. SELECT syntax. 您使用的是INSERT .. VALUES语法,而不是INSERT .. SELECT语法。 Your subquery provides values column1 but you don't provide any value for column2 . 您的子查询提供了column1值,但您没有为column2提供任何值。 What you want to do is described further down in the manual at "Example: INSERT SELECT syntax support". 手册“示例:INSERT SELECT语法支持”中进一步介绍了您要执行的操作。 In your case, this would read (jOOQ 2.x syntax, no longer available in jOOQ 3.x): 在您的情况下,这将显示为(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();

Or with a custom projection (jOOQ 3.x syntax): 或使用自定义投影(jOOQ 3.x语法):

create.insertInto(table1, column1, column2)
      .select(create.select(...))
      .execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM