我需要在 cte 中进行连接。 在文档中https://www.jooq.org/doc/latest/manual/sql-building/sql-statements/with-clause/只是简短的示例,没有任何
joins
和where
术语。
如果我使用
joins
的类型将是 TableOnConditionStep 但对于 org.jooq.DerivedColumnList4#as required Select 并面临 comile 时间错误
method as in interface org.jooq.DerivedColumnList4 cannot be applied to given types;
[ERROR] required: org.jooq.ResultQuery<R>
[ERROR] found: org.jooq.TableOnConditionStep<org.jooq.Record>
[ERROR] reason: cannot infer type-variable(s) R
[ERROR] (argument mismatch; org.jooq.TableOnConditionStep<org.jooq.Record> cannot be converted to org.jooq.ResultQuery<R>)
CommonTableExpression cte = name("amd")
.fields("contact_id", "created_date", "name", "type")
.as(
select(
HISTORIES.CONTACT_ID,
DSL.max(HISTORIES.CREATED_DATE),
CONTACTS.NAME,
CONTACTS.TYPE
).from(
dslContext.select(
DSL.max(HISTORIES.ID),
DSL.max(HISTORIES.CONTACT_ID)
).from(HISTORIES)
.groupBy(HISTORIES.CONTACT_ID)
).asTable("q1")
.join(HISTORIES)
.on(field("q1.id").eq(HISTORIES.ID))
.join(CONTACTS)
.on(DSL.coalesce(HISTORIES.CREATED_BY, HISTORIES.CONTACT_ID).eq(CONTACTS.ID))
);
你放错了括号。你写道:
).asTable("q1")
当你应该写的时候
.asTable("q1"))
当您将派生表分配给局部变量时,它会变得更加明显,如下所示:
Table<?> q1 =
select(max(HISTORIES.ID), max(HISTORIES.CONTACT_ID))
.from(HISTORIES)
.groupBy(HISTORIES.CONTACT_ID)
.asTable("q1");
然后:
...
.from(q1)
.join(HISTORIES)
...