当前位置: 首页 > 知识库问答 >
问题:

QueryDSL select count()/count()how~?

范承志
2023-03-14

选择

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

共有1个答案

虞祯
2023-03-14

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

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")
 类似资料:
  • 本文向大家介绍count(*),count(1)和count(列名)的区别?相关面试题,主要包含被问及count(*),count(1)和count(列名)的区别?时的应答技巧和注意事项,需要的朋友参考一下 count(*),count(1)在统计的时候不会忽略Null,count(列名)在统计的时候会忽略Null。若列名为主键,count(列名)会比count(1),count(*)快,反之则c

  • 要查找字符串中的字符数,可以使用count函数。 语法 (Syntax) 以下是语法。 (count stringvariable) Parameters - 'Stringvariable,是需要确定字符数的字符串。 Return Value - 字符串中的字符数。 例子 (Example) 以下是Clojure中字符串格式的示例。 (ns clojure.examples.hello

  • 问题内容: 只是想知道您是否有人用光了,性能是否存在明显差异,或者这仅仅是过去的日子所养成的传统习惯? 具体的数据库是。 问题答案: 没有区别。 原因: 在线书籍上说“ ” “ 1”是非空表达式:因此与相同。优化器可以识别它是什么:琐碎的。 与或相同 例子: 相同的IO,相同的计划,作品 编辑,2011年8月 关于DBA.SE的类似问题。 编辑,2011年12月 在ANSI-92中专门提到(请查找

  • 问题内容: 假设表中有一个主字段“ id”(例如速度等),以下查询之间是否有区别? 与 问题答案: 在www.mysqlperformanceblog.com上查看Count(*)与Count(col),他们讨论了有关各种“ col”类型(是否为NULL,是否不带索引,带索引等)的该主题,以及针对MyISAM和InnoDB表的主题。

  • 说明: int Worker::$count 设置当前Worker实例启动多少个进程,不设置时默认为1。 如何设置进程数,请参考这里 。 注意:此属性必须在Worker::runAll();运行前设置才有效。windows系统不支持此特性。 范例 use WorkermanWorker; require_once __DIR__ . '/Workerman/Autoloader.php'; $

  • 计算符合条件的记录数量。 function Model::count(array $params); 使用实例 $model->count(array( 'where' => array( 'id > 5', "name='test' ", ), "group"=>"test_id", 'level' => 100, )); 调用