我如何为以下 MSSQL 查询编写 JOOQ select 查询?
How do i write JOOQ select query for the following MSSQL query?
如何将以下 select 查询从 MSSQL 转换为 JOOQ 格式?
MySql查询:
SELECT group_concat(AUF.AcLineId )
FROM AcHeader as AH
INNER JOIN AcDetails AS AD
ON AD.AcHeaderId = AH.AcHeaderId
INNER JOIN AcUserField AS AUF
ON AUF.AcLineId = AD.AcLineId
AND AUF.FieldNo = 1
WHERE AH.Company = 1 AND AH.AccountNo = 190000 AND AH.Status IN ('C', 'D')
Jooq查询:
dslContext.select(AUF.AcLineId)
.from(AcHeader)
.Join(AcDetails)
.on(AcDetails.AcHeaderId.equal(AcHeader.AcHeaderId))
.Join(AcUserField )
.on(AcUserField.AcLineId.equal(AcDetails.AcLineId))
.and(AcUserField.FieldNo.equal())
.Where(AcHeader.Company=1)
.and(AcHeader=AccountNo=190000).and(Status.in("'C','D'")).fetch()
我想也完成了,但在这里我很难形成 group_concat。
如果有想法,请与我分享。
jOOQ 支持 MySQL 的 GROUP_CONCAT()
函数作为 DSL.groupConcat()
。这是您更正的查询和评论:
// Don't forget, of course:
import org.jooq.impl.DSL;
然后
// Use DSL.groupConcat() here
dslContext.select(DSL.groupConcat(AcUserField.AcLineId))
.from(AcHeader)
// Lower-case join()
.join(AcDetails)
.on(AcDetails.AcHeaderId.equal(AcHeader.AcHeaderId))
// Lower-case join()
.join(AcUserField)
.on(AcUserField.AcLineId.equal(AcDetails.AcLineId))
// Forgot to pass the parameter "1"
.and(AcUserField.FieldNo.equal(1))
// Lower-case where() and use .equal() or .eq() rather than a Java assignment "="
.where(AcHeader.Company.equal(1))
.and(AcHeader.AccountNo.equal(190000))
// List values in in(...) predicate individually, don't put them all
// in a single string.
.and(AcHeader.Status.in("C", "D")).fetch()
可选地,如果您更喜欢使用表格的别名版本,您可以这样做:
AcHeader AH = AcHeader.as("AH");
AcDetails AD = AcDetails.as("AD");
AcUserField AUF = AcUserField.as("AUF");
dslContext.select(DSL.groupConcat(AUF.AcLineId))
.from(AH)
.join(AD)
.on(AD.AcHeaderId.equal(AcHeader.AcHeaderId))
.join(AUF)
.on(AUF.AcLineId.equal(AD.AcLineId))
.and(AUF.FieldNo.equal(1))
.where(AH.Company.equal(1))
.and(AH.AccountNo.equal(190000))
.and(AH.Status.in("C", "D")).fetch()
如何将以下 select 查询从 MSSQL 转换为 JOOQ 格式?
MySql查询:
SELECT group_concat(AUF.AcLineId )
FROM AcHeader as AH
INNER JOIN AcDetails AS AD
ON AD.AcHeaderId = AH.AcHeaderId
INNER JOIN AcUserField AS AUF
ON AUF.AcLineId = AD.AcLineId
AND AUF.FieldNo = 1
WHERE AH.Company = 1 AND AH.AccountNo = 190000 AND AH.Status IN ('C', 'D')
Jooq查询:
dslContext.select(AUF.AcLineId)
.from(AcHeader)
.Join(AcDetails)
.on(AcDetails.AcHeaderId.equal(AcHeader.AcHeaderId))
.Join(AcUserField )
.on(AcUserField.AcLineId.equal(AcDetails.AcLineId))
.and(AcUserField.FieldNo.equal())
.Where(AcHeader.Company=1)
.and(AcHeader=AccountNo=190000).and(Status.in("'C','D'")).fetch()
我想也完成了,但在这里我很难形成 group_concat。 如果有想法,请与我分享。
jOOQ 支持 MySQL 的 GROUP_CONCAT()
函数作为 DSL.groupConcat()
。这是您更正的查询和评论:
// Don't forget, of course:
import org.jooq.impl.DSL;
然后
// Use DSL.groupConcat() here
dslContext.select(DSL.groupConcat(AcUserField.AcLineId))
.from(AcHeader)
// Lower-case join()
.join(AcDetails)
.on(AcDetails.AcHeaderId.equal(AcHeader.AcHeaderId))
// Lower-case join()
.join(AcUserField)
.on(AcUserField.AcLineId.equal(AcDetails.AcLineId))
// Forgot to pass the parameter "1"
.and(AcUserField.FieldNo.equal(1))
// Lower-case where() and use .equal() or .eq() rather than a Java assignment "="
.where(AcHeader.Company.equal(1))
.and(AcHeader.AccountNo.equal(190000))
// List values in in(...) predicate individually, don't put them all
// in a single string.
.and(AcHeader.Status.in("C", "D")).fetch()
可选地,如果您更喜欢使用表格的别名版本,您可以这样做:
AcHeader AH = AcHeader.as("AH");
AcDetails AD = AcDetails.as("AD");
AcUserField AUF = AcUserField.as("AUF");
dslContext.select(DSL.groupConcat(AUF.AcLineId))
.from(AH)
.join(AD)
.on(AD.AcHeaderId.equal(AcHeader.AcHeaderId))
.join(AUF)
.on(AUF.AcLineId.equal(AD.AcLineId))
.and(AUF.FieldNo.equal(1))
.where(AH.Company.equal(1))
.and(AH.AccountNo.equal(190000))
.and(AH.Status.in("C", "D")).fetch()