QueryDSL选择count/ count怎么样〜?

问题描述

选择

      (select count(*) from table2 where table2.table2Seq = table1.table1Seq) as count1,(select count(*) from table2 where table2.table2Seq = table1.table1Seq and table2Yn = true) as count2,(select count(*) from table2 where table2.table2Seq = table1.table1Seq) / (select count(*) from table2 where table2.table2Seq = table1.table1Seq and table2Yn = true) as count3

来自

      table1

订购

     count3

**我想要这个查询。您如何使其成为queryDSL?我想对子查询中的计数器除以子查询中的计数器。 **

NumberPath<Long> count1 = Expressions.numberPath(Long.class,“count1”);
NumberPath<Long> count2 = Expressions.numberPath(Long.class,“count2”);
NumberPath<Long> count3 = Expressions.numberPath(Long.class,“count3”);

JPQLQuery<OnlineTrainingCourseDto> query = getQuerydsl().createquery()
    .select(
        new Dto(
            ExpressionUtils.as(
                JPAExpressions.select(table2.table2Seq.count())
                        .from(table2)
                        .where(table2.table2Seq.eq(table1.table1Seq)),count1
            ),ExpressionUtils.as(
                JPAExpressions.select(table2.table2Seq.count())
                        .from(table2)
                        .where(table2.table2Seq.eq(table1.table1Seq)
                        .and(table2.table2Yn.eq(true))),count2
            ),***????? count1 / count2 as count3 ?????***

            )
        )
    )
    .from(table1)

解决方法

您可以将表达式转换为数字表达式以访问除法运算符:

JPAExpressions.asNumber(
     JPAExpressions.select(table2.table2Seq.count())
                        .from(table2)
                        .where(table2.table2Seq.eq(table1.table1Seq)))
.divide(
    JPAExpressions.asNumber(JPAExpressions.select(table2.table2Seq.count())
                        .from(table2)
                        .where(table2.table2Seq.eq(table1.table1Seq)
                        .and(table2.table2Yn.eq(true))))
).as("count3")

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...