Doctrine2限制行在查询中求和

时间:2016-02-08 12:56:24

标签: doctrine-orm

我有一个返回总和的查询,但我想限制要查询的行数,但是不起作用。

这是我的疑问:

    $qb = $this->createQueryBuilder('result')
        ->select('SUM(result.generalPoints) AS generalPoints, SUM(result.coefficient) AS coefficient')
        ->join('result.inscription', 'inscription', Join::WITH, 'inscription.user = :user')
        ->join('inscription.race', 'race')
        ->join('inscription.category', 'category', Join::WITH, 'category.generalRanking = true')
        ->join('race.event', 'event')
        ->join('event.competitionSeason', 'competitionSeason', Join::WITH, 'competitionSeason = :competitionSeason')
        ->orderBy('generalPoints', 'DESC')
        ->addOrderBy('coefficient', 'DESC')
        ->setParameter('competitionSeason', $competitionSeason)
        ->setParameter('user', $user);

    if (isset($limit)) {
        $qb->setMaxResults($limit);
    }

    return $qb->getQuery()->getOneOrNullResult();

有些想法?

由于

1 个答案:

答案 0 :(得分:1)

由于sum是一个聚合函数,其结果是一行,之后会应用limit语句。您需要创建子查询,使用generalPointscoefficient生成多个行,限制此子查询中的行数,并在包装​​查询中使用聚合函数。