如何为以下MSSQL查询编写JOOQ select查询?

时间:2015-08-21 10:47:29

标签: hibernate spring-mvc jooq

如何将以下选择查询从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。 如果有想法,请与我分享。

1 个答案:

答案 0 :(得分:1)

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()