简体   繁体   English

Doctrine DQL查询到Mysql查询语法错误

[英]Doctrine DQL query to Mysql query syntax error

I am having a hard time finding out why this Doctrine dql query is not working in my symfony application.我很难找出为什么这个 Doctrine dql 查询在我的 symfony 应用程序中不起作用。

Mysql query is this: Mysql查询是这样的:

SELECT 
    (COUNT(CASE WHEN c_email IS NOT NULL THEN 1 END) * 100.00) / COUNT(c_number) AS percentage
FROM 
    distinct_customers;

My Symfony doctrine php code is this我的 Symfony 学说 php 代码是这样的

   public function invalidEmails()
    {
        $em = $this->getEntityManager();
        $qb = $em->createQuery('
            SELECT (count(case  when  ds.c_email IS NOT null then 1 end))*100/count(ds.c_number) as percentage FROM App\Entity\DistinctCustomers ds');
        return $qb->getResult();
    }

But I get an error each time但我每次都会出错

[Syntax Error] line 0, col 69: Error: Expected Doctrine\\ORM\\Query\\Lexer::T_ELSE, got 'end'

has someone ran into this in the past?过去有人遇到过这种情况吗?

Your CASE block needs an ELSE condition.您的CASE块需要一个ELSE条件。

In addition, it looks like you're trying to count the cases where email is not null, but instead of using the COUNT function (which would count nulls and zeroes as well as 1 's) you need to use the SUM function.此外,看起来您正在尝试计算电子邮件不为空的情况,但您需要使用SUM函数而不是使用COUNT函数(它将计算nullszeroes以及1 )。

Try this:尝试这个:

SELECT(
    SUM(
        CASE
        WHEN ds.c_email IS NOT null
        THEN 1
        ELSE 0
        END
    )
)*100/COUNT(ds.c_number) AS percentage
FROM App\Entity\DistinctCustomers ds

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM