Doctrine 2 DQL CASE WHEN in Count

Doctrine 2 DQL CASE WHEN in Count(Doctrine 2 DQL CASE WHEN in Count)
本文介绍了Doctrine 2 DQL CASE WHEN in Count的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本机 MySQL 代码中有这个查询

I have this Query in native MySQL Code

SELECT *
FROM `turn`
LEFT JOIN (
    poi
) ON ( turn.id = poi.turn_id )
GROUP BY turn.id
ORDER BY count( case when poi.image = 1 then 1 else null end) DESC;

我需要在 Doctrine 2 DQL 中重建它

I need to rebuild this in Doctrine 2 DQL

到目前为止我的尝试是这样的:

My attempt so far is this:

SELECT t, COUNT((CASE WHEN BundleEntityPoi p.image = 1 then 1 ELSE NULL END)) AS num
FROM BundleEntityTurn t
JOIN t.pois p
GROUP BY t.id
ORDER BY num DESC

我收到此错误:

在 Bundle:Admin:showTurnsFiltered.html 中渲染模板时抛出异常([语法错误] line 0, col 99: Error: Expected end of string, got '.'").twig 在第 75 行.

我做错了什么?

推荐答案

经过数小时的尝试和搜索,我自己找到了它,它与此 DQL 配合使用:

I found it by myself after hours of trying and searching, it's working with this DQL:

$dql = 'SELECT t, SUM(CASE WHEN p.image = 1 THEN 1 ELSE 0 END) AS numImage
                    FROM BundleEntityTurn t
                    JOIN t.pois p
                    GROUP BY t.id
                    ORDER BY numImage DESC;  

重要的是您需要使用 SUM 而不是 COUNT

Important that you need to use SUM instead of COUNT

这篇关于Doctrine 2 DQL CASE WHEN in Count的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)