我正在尝试为唯一用户选择最大分数对象。
所以用户应该是不同的,我应该收到用户的最大分数(订单),我需要获得这个最大分数的其他对象值,如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();不管我怎么尝试,我都不能让distinct工作。我尝试过group by MAX(s.score),但这会将随机对象与最大得分关联起来,而不是将相同的对象关联起来。
有谁有解决这个问题的办法吗?Distinct似乎根本不起作用……
生成的查询:
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值得一提的是,用户是一种关系。当我在其他文件上尝试此功能时,它工作正常...
发布于 2014-03-09 03:38:43
如果需要get count distinct值,可以使用countDistinct。但是,如果必须获取所有不同的值,请使用groupBy
https://stackoverflow.com/questions/16244013
复制相似问题