我有一个返回总和的查询,但我想限制要查询的行数,但是不起作用。
这是我的疑问:
$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();
有些想法?
由于
答案 0 :(得分:1)
由于sum
是一个聚合函数,其结果是一行,之后会应用limit语句。您需要创建子查询,使用generalPoints
和coefficient
生成多个行,限制此子查询中的行数,并在包装查询中使用聚合函数。