【问题标题】:Method orderby/group by/having not found in query jOOQ方法 orderby/group by/在查询 jOOQ 中未找到
【发布时间】:2018-05-24 08:19:24
【问题描述】:

我是 jOOQ 的新手,我必须将查询翻译成 jOOQ;

我的“CN_THEODOINO”表包含两个字段:NAM(表示 YEAR)和 THANG(表示 MONTH),数据类型为 short 和 byte。

我想计算该表中 sysdate 与 NAM 和 THANG 之间的不同月份。

之后,我将与我的参数中的 p_diff 进行比较并选择大于它的那些。

我试试这个:

public List<CN_DS_NO> layDsKHangNo3(String sInputParam) throws JSONException {
    try {
        Calendar c = Calendar.getInstance();
        int currMonths = c.get(Calendar.YEAR) * 12 + c.get(Calendar.MONTH);
        JSONObject jsonObj = new JSONObject(sInputParam);
        short p_diff    =    (short)jsonObj.getInt("SO_THANGNO");
        Condition condition = DSL.trueCondition();
        condition = condition.and(bangDsChuyenNo2.TIEN_NO.gt(Long.valueOf(0)));
        condition = condition.and(bangDsChuyenNo2.NAM.mul(12).add(bangDsChuyenNo2.THANG).add(p_diff).sub(currMonths).lessOrEqual((short)0));
        condition = condition.groupBy(bangDsChuyenNo2.MA_KHANG);
        condition = condition.having(DSL.count(bangDsChuyenNo2.ID_HDON).gt(jsonObj.getInt("SO_HOADON")).and(DSL.sum((Field) field(bangDsChuyenNo2.TIEN_NO)).gt(jsonObj.getLong("TONG_NO"))));

        qDsChuyenNo = this.getCreate()
                        .select()
                        .from(bangDsChuyenNo)
                        .where(condition);
        return nativeQuery(this.getEm(), qDsChuyenNo, CN_DS_NO.class);

            } catch (Exception ex) {
            throw ex;
            }

我得到两个错误,比如说:

错误(141,34):找不到符号;符号:方法 groupBy(org.jooq.TableField); location:org.jooq.Condition 类型的变量条件

错误(142,34):找不到符号;符号:方法有(org.jooq.Condition); location:org.jooq.Condition 类型的变量条件

那么我如何在 jOOQ 中做到这一点。我已经在谷歌和我们的论坛中搜索过,但无法得到答案。请帮帮我。

【问题讨论】:

    标签: java sql jooq


    【解决方案1】:

    org.jooq.Condition 类型为 SQL 谓词建模。它具有使用ANDOR 运算符组合谓词的方法,但肯定没有属于SELECT 查询的方法,例如GROUP BYHAVING。只需将这些子句移到它们所属的位置即可:

    Condition condition = DSL.trueCondition();
    condition = condition.and(bangDsChuyenNo2.TIEN_NO.gt(Long.valueOf(0)));
    condition = condition.and(bangDsChuyenNo2.NAM.mul(12).add(bangDsChuyenNo2.THANG).add(p_diff).minus(currMonths).greaterThan((short)0));
    
    // These calls make no sense at this position:
    // condition = condition.groupBy(bangDsChuyenNo2.MA_KHANG);
    // condition = condition.having(DSL.count(bangDsChuyenNo2.ID_HDON).gt(jsonObj.getInt("SO_HOADON")).and(DSL.sum((Field) field(bangDsChuyenNo2.TIEN_NO)).gt(jsonObj.getLong("TONG_NO"))));
    
    qDsChuyenNo = this.getCreate()
                      .select()
                      .from(bangDsChuyenNo)
                      .where(condition)
    
                      // Above clauses moved down here:
                      .groupBy(bangDsChuyenNo2.MA_KHANG)
                      .having(DSL.count(bangDsChuyenNo2.ID_HDON)
                                 .gt(jsonObj.getInt("SO_HOADON"))
                                 .and(DSL.sum((Field) field(bangDsChuyenNo2.TIEN_NO))
                                 .gt(jsonObj.getLong("TONG_NO"))))
                      ;
    

    【讨论】:

    • 非常感谢 Lukas 的洞察力。我还有一个问题。我是否必须写 bangDsChuyenNo2.NAM.mul(12).add(bangDsChuyenNo2.THANG).add(p_diff).minus(currMonths) 这意味着 bangDsChuyenNo2.NAM + bangDsChuyenNo2.THANG + p_diff - currMonths 而不是 currMonths - p_diff - bangDsChuyenNo2 .NAM * 12 - bangDsChuyenNo2.THANG。似乎第二个更自然(对我来说)?
    • 拜托,@NghịLê:你能问一个新问题吗? Stack Overflow 不会很快用完问题 ID。在这些 cmets 中很难回答问题...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    相关资源
    最近更新 更多