繁体   English   中英

Symfony 2 Multiple Selects在同一张桌子上有计数吗?

[英]Symfony 2 Multiple Selects with counts on the same table?

好的,我在一张桌子上有一个标志字段,打开或关闭都是布尔值。 我正在尝试建立一个查询,该查询将采用该字段并根据该标志对其进行计数。 然后,我需要按帐户ID对它们进行分组

这就是我现在正在处理的

   $GetTest1 = $GetRepo->createQueryBuilder('s') <- I had 'w' in here but all that did was add an index and not a second alias?
                                ->select(' (count(s.open_close)) AS ClosedCount, (count(w.open_close)) AS OpenCount ')
                                ->where('s.open_close = ?1')
                                //->andWhere('w.open_close = ?2')
                                ->groupBy('s.AccountID')
                                ->setParameter('1', true)
                                //->setParameter('2', false)
                                ->getQuery(); 

我想要的是可行的吗? 我知道(或至少认为是这样)可以构建具有多个表别名的查询吗? -如果我错了,请纠正我。

所有帮助都非常欢迎。

谢谢

此DQL查询将按accountId对表中的行进行分组,对于每个行,它将为您计数为是(您可以通过从总数中减去该值来获得计数)。 顺便说一句,我发现编写直接的DQL查询比编写QueryBuilder查询要简单得多(仅当我需要动态构造查询时才使用它)

$results = $this->get("doctrine")->getManager()
    ->createQuery("
    SELECT t.accountId, SUM(t.openClose) as count_yes, COUNT(t.accountId) as total
    FROM AppBundle:Table t 
    GROUP BY t.accountId
    ")
    ->getResult();
foreach ($results as $result) {
    //echo print_r($result);
    //you can get count_no as $result["total"] - $result["count_yes"];
}

暂无
暂无

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

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