一些在教义查询语言中的顺序

时间:2013-03-27 12:04:44

标签: doctrine-orm doctrine

我的查询如下:

SELECT t.id, t.name, count(i.id) FROM Acme\TestBundle\Entity\Tags t
LEFT JOIN t.items i WITH i.status=1 AND (SELECT count(img.id) FROM Acme\TestBundle\Entity\ItemsImages img WHERE img.item=i.id) > 0
GROUP BY t.id
ORDER BY COUNT (i.id) DESC

此查询在没有ORDER BY子句的情况下正常工作。每当我添加这个子句时,它都会给我错误:

[Syntax Error] line 0, col 297: Error: Expected end of string, got '(' 

ORDER BY与列名一起使用,但是像count(i.id)这样的列不起作用

1 个答案:

答案 0 :(得分:0)

要按汇总值排序,您需要先SELECT,然后将其用于ORDER BY

SELECT
    t.id,
    t.name,
    count(i.id) AS tags_count
FROM 
    Acme\TestBundle\Entity\Tags t
...
ORDER BY
    tags_count DESC