1

我有一个查询如下:

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) 之类的列不起作用

4

1 回答 1

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
于 2013-03-27T14:17:02.530 回答