【问题标题】:How to orderby using alias in jooq如何在 jooq 中使用别名进行排序
【发布时间】:2021-06-10 22:12:27
【问题描述】:

在下面的示例中,我如何在 jooq 中 orderBy “amount”(这是我使用子查询创建的别名)?我无法理解如何阅读文档。

    return context
            .select(
                    NAME,
                    sum(
                            COLUMN_ONE.minus(COLUMN_TWO)
                    ).as("amount")
            )
            .from(MYTABLE)
            .groupBy(MYTABLE.NAME)
            .fetchInto(MyClass.class);

【问题讨论】:

    标签: java jooq


    【解决方案1】:

    好的,我自己找到了解决方案。为此,有必要创建一个字段:

    Field<BigDecimal> amount = field(
                    sum(
                         COLUMN_ONE.minus(COLUMN_TWO)
                    ).as("amount")
            );
    

    最后在您的原始查询中使用它:

        return dslContext
                .select(
                        TRANSACTIONS.COIN_NAME,
                        amount
                )
                .from(MYTABLE)
                .groupBy(MYTABLE.NAME)
                .orderBy(coinAmount.desc())
                .fetchInto(MyClass.class);
    

    【讨论】:

    • 不需要使用DSL.field()包装这个表达式
    猜你喜欢
    • 2012-08-05
    • 1970-01-01
    • 2022-10-06
    • 2012-09-17
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    相关资源
    最近更新 更多