sql-order-by - 如何在《教义2》中按数排序?

标签 sql-order-by doctrine-orm query-builder

我试图按一个字段(年份)对我的实体进行分组并对其进行计数。

码:

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')值?

最佳答案

使用COUNT(b.id) AS count时出现什么错误?可能是因为count是保留字。尝试COUNT(b.id) AS idCount或类似的方法。

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

您的数据库系统是什么(mysql,postgresql等)?

关于sql-order-by - 如何在《教义2》中按数排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6000622/

相关文章:

php - 如何正确使用 Union 和 Order BY?

MySQL如何限制第一个 "order by"结果

php - Symfony 主义数据库规范化

php - Doctrine 中的两个生成值

php - 如何在 laravel Eloquent 查询构建器中正确执行查询

MySQL 以 5 分钟为间隔订购 block

sql - ORDER BY 输出列小写

php - 使用 PHPUnit 在 doctrine2 中模拟 findOneBy"field"

mysql - Laravel 查询生成器 ->= 全部(选择...)

JQuery QueryBuilder 不工作