gpt4 book ai didi

java - jOOQ 幂等批量插入

转载 作者:行者123 更新时间:2023-12-04 15:10:13 24 4
gpt4 key购买 nike

我正在尝试实现一个幂等插入,这个查询看起来很适合这个任务

insert into test_table
select *
from (
values (1, 2, 3, 4),
(3, 4, 5, 6),
(1, 2, 3, 4),
(3, 4, 5, 6)
) as data(a, b, c, d)
where not exists(select 1
from test_table
where test_table.a = data.a
and test_table.b = data.b
and test_table.c = data.c);

请帮助将此查询转换为 jOOQ DSL
我使用了 Greenplum 数据库并且不支持 ON CONFLICT 语法

最佳答案

ctx.insertInto(TEST_TABLE)
.select(
select()
.from(values(
row(1, 2, 3, 4),
row(3, 4, 5, 6),
row(1, 2, 3, 4),
row(3, 4, 5, 6)
).as("data", "a", "b", "c", "d"))
.whereNotExists(
selectOne()
.from(TEST_TABLE)
.where(TEST_TABLE.A.eq(field(name("data", "a"), TEST_TABLE.A.getDataType())))
.and(TEST_TABLE.B.eq(field(name("data", "b"), TEST_TABLE.A.getDataType())))
.and(TEST_TABLE.C.eq(field(name("data", "c"), TEST_TABLE.A.getDataType())))
)
)
.execute();

此答案假设您正在为 TEST_DATA 使用代码生成器(否则,请手动构建您的标识符,如上所示的 name("data", "a")or as shown here)。此外,它假设:

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

When Greenplum is formally supported, see #4700 ,然后可以为您模拟 ON CONFLICTON DUPLICATE KEY IGNORE

关于java - jOOQ 幂等批量插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65323651/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com