我在下面有此查询,该查询返回所有预期的结果。但是,运行时会出现教义语法错误,但无法查明。有一种特殊的方法可以在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可以将它们分开吗?
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))
这是通过的查询
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();
}