如何在dql中选择AVG?

时间:2016-12-01 05:09:56

标签: php mysql symfony doctrine-orm dql

我想在dql中获得平均值但我没有运气。

    $qb = $this->em->getRepository($this->target)->createQueryBuilder('m');         
    $qb
        ->select($qb->expr()->avg('m.total_online as ACU'))
        ->where('m.server = :server')
        ->andWhere('m.time LIKE :time')
        ->setParameter('server', $server)
        ->setParameter('time',$date.'%');           
        $rs= $qb->getQuery()
        ->getResult();

但它不会像我预期的那样给我结果:

+--------+
| ACU    |
+--------+
| 0.1429 |
+--------+

我的sql是:

SELECT AVG(Total_Online) as ACU FROM `monitor` WHERE time like '2016-12-01%' AND server='$x'

有人能帮帮我吗? 感谢

1 个答案:

答案 0 :(得分:0)

我在这里得到了答案:

$qb = $this->em->createQueryBuilder();
$rs = $qb
        ->select($qb->expr()->avg('m.total_online'))
        ->from($this->target,'m')
        ->where('m.server = :server')
        ->andWhere('m.time LIKE :time')
        ->setParameter('server', $server)
        ->setParameter('time',$date.'%')            
        ->getQuery()
        ->getResult();

希望对某人有所帮助。