如何在 Doctrine 2 中按计数排序? [英] How to order by count in Doctrine 2?

查看:16
本文介绍了如何在 Doctrine 2 中按计数排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按字段(年份)对我的实体进行分组并对其进行计数.

I'm trying to group my entity by a field (year) and do a count of it.

代码:

public function countYear()
{
    $qb = $this->getEntityManager()->createQueryBuilder();
    $qb->select('b.year, COUNT(b.id)')
        ->from('MyEntityAlbum', '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') 值?

I can't seem to say COUNT(b.id) AS count as it gives an error, and I do not know what to use as the addOrderby(???, 'DESC') value?

推荐答案

你在使用 COUNT(b.id) AS count 时遇到的错误是什么?这可能是因为 count 是一个保留字.尝试 COUNT(b.id) AS idCount 或类似方法.

what is the error you get when using COUNT(b.id) AS count? it might be because count is a reserved word. try COUNT(b.id) AS idCount, or similar.

或者,尝试 $qb->addOrderby('COUNT(b.id)', 'DESC');.

你的数据库系统是什么(mysql、postgresql、...)?

what is your database system (mysql, postgresql, ...)?

这篇关于如何在 Doctrine 2 中按计数排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆