我试图按照一个字段(年份)对实体进行分组,并统计其数量。
代码:
public function countYear()
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('b.year, COUNT(b.id)')
->from('\My\Entity\Album', 'b')
->where('b.year IS NOT NULL')
->addOrderBy('sclr1', 'DESC')
->addGroupBy('b.year');
$query = $qb->getQuery();
die($query->getSQL());
$result = $query->execute();
//die(print_r($result));
return $result;
}
我无法使用COUNT(b.id) AS count
,因为它会报错,并且我不知道在addOrderby(???, 'DESC')
中应该使用什么值?
AS HIDDEN
对我非常有帮助。 - Peter Bailey