学说不同createQueryBuilder

问题描述 投票:0回答:1

我想选择唯一的用户(一个或多个)MAX得分的对象。

因此,用户应该是不同的,我应该得到最高分的用户(顺序),我需要得到像其他dataAdded对象值这个最高分。

    $query = $this->createQueryBuilder('s');
    $query->select('DISTINCT s.user, s');
    $query->where('s.challenge = :challenge')->setParameter('challenge', $challenge);

    $query->andWhere('s.date_added BETWEEN :monday AND :sunday')
       ->setParameter('monday', $startDate->format('Y-m-d'))
       ->setParameter('sunday', $endDate->format('Y-m-d'));

    $query->orderBy('s.score', 'DESC');

    //$query->groupBy('s.user');
    $query->setMaxResults($limit);

    $results = $query->getQuery();

不要紧,我如何努力,我不能让不同的工作。我已经MAX(s.score)试图群组,但这将随机对象,具有最高分而不是同一个对象关联..

有没有人有一个解决的办法?不同的只是似乎没有工作...

生成的查询:

SELECT DISTINCT s.user, s FROM Digital\ApplicationBundle\Entity\ChallengeScore s WHERE s.challenge = :challenge AND (s.date_added BETWEEN :monday AND :sunday) ORDER BY s.score DESC

但是一个错误;(

[Semantical Error] line 0, col 18 near 'user, s FROM': Error: Invalid PathExpression. Must be a StateFieldPathExpression

它值得一提的是用户的关系。当我尝试此上的其他文件能正常工作?

doctrine-orm doctrine distinct symfony-2.1
1个回答
0
投票

如果你需要得到数不同的值,你可以使用countDistinct。但是,如果你必须让所有不同值,使用groupBy

© www.soinside.com 2019 - 2024. All rights reserved.