在具有多个SELECT的Doctrine CreateQueryBuilder上获取语法错误

问题描述 投票:0回答:2

我在下面有此查询,该查询返回所有预期的结果。但是,运行时会出现教义语法错误,但无法查明。有一种特殊的方法可以在Doctrine中添加多个选择吗?

这是我的职能。

public function summaryReport()
{
    $qb = $this->createQueryBuilder('ds')
        ->select('ds.d_id, ds.d_name,
                        SUM(ds.c_email IS NOT NULL) * 100.00 / COUNT(c_number) AS percentage,
                        COUNT(DISTINCT ds.c_number) AS Qualifying_customers,
                        sum(ds.c_email IS not NULL) AS Qualifying_Customers_with_Email')
        ->groupBy('ds.d_id, ds.d_name')
        ->getQuery();
    dd($qb->getDQL());
    return $qb->getResult();
}

我收到的错误是

 Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got 'IS'

addSelect可以将它们分开吗?

php orm doctrine symfony4 query-builder
2个回答
0
投票

SUM(ds.c_email IS NOT NULL)在这里不正确。SUM函数定义为"SUM" "(" ["DISTINCT"] StringPrimary ")",此处的条件不接受。

如果要使NOT NULL条件返回1或0,则必须使用IF函数。例如:SUM(IF(ds.c_email IS NOT NULL, 1, 0))


0
投票

这是通过的查询

public function summaryReport()
{
    $qb = $this->createQueryBuilder('ds')
        ->select('ds.d_id, ds.d_name,
                        (sum(case  when  ds.c_email IS null then 0 else 1 end))*100/count(ds.c_number) as percentage')
        ->addSelect('COUNT(DISTINCT ds.c_number) AS qualifying')
        ->addSelect('sum(case when ds.c_email IS NULL then 0 else 1 end) AS qualifyingEmail')
        ->groupBy('ds.d_id, ds.d_name')
        ->getQuery();

    return $qb->getResult();
}
© www.soinside.com 2019 - 2024. All rights reserved.