jooq mysql,在带有JOOQ的MySQL中使用默认值插入.. SELECT

Lets say I have a table Person(id, fname, lname) and it contains a record (1, 'Michael', 'Bay'). Now I wish to create another record in Person table with the same fname and lname but different id i.e. (453456, 'Michael', 'Bay'). This is the way I would do in plain SQL

INSERT INTO Person(id, fname, lname)

SELECT 453456, Person.fname, Person.lname

FROM Person

WHERE Person.id = 1;

How can this be done with JOOQ (ideally while retaining JOOQ's code generation and type safety features)?

I am aware that JOOQ provides ability to copy entire records from one table to same or another table with its selectFrom syntax

jooq.insertInto(PERSON)

.select(selectFrom(PERSON).where(PERSON.ID.eq(1)))

.execute();

But in my case, there are only certain columns in the record that I need to copy while the rest of the values needed to be set explicitly

Another solution I could think of is the following

jooq.insertInto(PERSON)

.values(452452)

.execute();

jooq.update(PERSON)

.set(row(PERSON.FNAME, PERSON.LNAME),

select(PERSON.FNAME, PERSON.LNAME)

.from(PERSON)

.where(PERSON.ID.eq(1)))

.where(PERSON.ID.eq(452452))

.execute();

But it doesn't feel right. I would be grateful if someone could provide any other solution/workaround for this problem.

解决方案

jooq.insertInto(PERSON)

.columns(PERSON.ID, PERSON.FNAME, PERSON.LNAME)

.select(select(val(452452), PERSON.FNAME, PERSON.LNAME)

.from(PERSON)

.where(PERSON.ID.eq(1)))

.execute();

As always, this is assuming the following static import:

import static org.jooq.impl.DSL.*;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值