JOOQ delete with join

问题描述

如何编写 JOOQ 删除查询以连接 with 子句中的字段?可能吗?

我有一个可用的 sql 查询,但我不知道如何将其转换为 JOOQ。

这是我的查询

with "temp" as (
    select field1 as field1
    from "table"
             join ("table2")
                  on (table.field1 = table2.field2)
)
delete from table using temp
where table.field1 = temp.field1;

我试过了:

transactionDSLContext.with("temp")
dsl.as(
                                    select(TABLE.FIELD1.as("field1"))
                                            .from(TABLE)
                                            .join(TABLE2)
                                            .on(TABLE.FIELD1.eq(TABLE2.FIELD2))

                            )
                            .delete(TABLE)
                            .where(TABLE.FIELD1.eq((Field<String>) temp.field("field1"))
                            .execute();

但我得到:

ERROR: missing FROM-clause entry for table "temp"

解决方法

您忘记了 using 子句:

.delete(TABLE).using(temp)

我假设您有一个局部变量 temp,您在其中分配了 CommonTableExpression...