【问题标题】:Sum of multiple columns using jooq and join with another table使用 jooq 对多列求和并与另一个表连接
【发布时间】:2019-05-16 09:20:42
【问题描述】:

我有一个场景,我必须从 table2 中找到多个列的总和,并且我在另一个 table1 的 where 子句中有值。

我为以下相同编写了 mySql 查询。我需要把它写在jooq中。

select (sum(t2.column1)+sum(t2.column2)+sum(t2.column3)) as total_amount 
from db.table1 t1, db.table2 t2 
where t1.column1 = ‘value1’ and t1.column2 = t2.column4;

【问题讨论】:

    标签: mysql jooq


    【解决方案1】:

    作为一般经验法则,org.jooq.impl.DSL 中的所有函数都可以使用相同的名称,所有运算符都可以从org.jooq.Field 中使用,名称反映了运算符的发音方式。在你的情况下,使用:

    具体来说,假设这个静态导入:

    import static org.jooq.impl.DSL.*;
    

    写:

    Table1 t1 = TABLE1.as("t1");
    Table2 t2 = TABLE2.as("t2");
    ctx.select(sum(t2.COLUMN1).plus(sum(t2.COLUMN2)).plus(t2.COLUMN3)).as("total_amount"))
       .from(t1, t2)
       .where(t1.COLUMN1.eq("value1"))
       .and(t1.COLUMN2.eq(t2.COLUMN4))
       .fetch();
    

    【讨论】:

      猜你喜欢
      • 2021-10-09
      • 1970-01-01
      • 1970-01-01
      • 2021-01-14
      • 1970-01-01
      • 2018-01-18
      • 1970-01-01
      • 2020-05-25
      • 1970-01-01
      相关资源
      最近更新 更多