简体   繁体   English

Symfony学说的多重计数

[英]Symfony doctrine multiple count

I try to retrieve multiple count in a single query on a single table. 我尝试在单个表的单个查询中检索多个计数。

return $this->getEntityManager()->createQuery(
        'SELECT COUNT(c1) AS enabled, COUNT(c2) AS disabled, COUNT(c3) AS locked
        FROM AcmeUserBundle:User c1, AcmeUserBundle:User c2, AcmeUserBundle:User c3
        WHERE c1.enabled = 1
        AND c2.enabled = 0
        AND c3.locked = 1'
    )->getSingleResult();

With this action I get this result: 通过此操作,我得到以下结果:

array [
  "enabled" => "4"
  "disabled" => "4"
  "locked" => "4"
]

The result that I expected would be this: 我期望的结果是这样的:

array [
  "enabled" => "2"
  "disabled" => "1"
  "locked" => "1"
]

I get that a simple count of the entire table. 我得到了整个表的简单计数。

Some body knows how i can make it? 有些人知道我该怎么做?

SQL query is wrong. SQL查询错误。 You are doing cross join of three tables, but you want to count rows that satisfy some condition in the table: 您正在对三个表进行交叉联接,但是您要对满足表中某些条件的行进行计数:

SELECT SUM(CASE WHEN u.enabled = 1 THEN 1 ELSE 0 END) as enabled,
       SUM(CASE WHEN u.enabled = 0 THEN 1 ELSE 0 END) as disabled,
       SUM(CASE WHEN u.locked = 1 THEN 1 ELSE 0 END) as locked
FROM AcmeUserBundle:User u

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

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