SQL Group By with an Order By

前端 未结 6 2252
时光取名叫无心
时光取名叫无心 2020-11-28 03:08

I have a table of tags and want to get the highest count tags from the list.

Sample data looks like this

id (1) tag (\'night\')
id (2) tag (\'awesome         


        
6条回答
  •  一个人的身影
    2020-11-28 03:34

    MySQL prior to version 5 did not allow aggregate functions in ORDER BY clauses.

    You can get around this limit with the deprecated syntax:

    SELECT COUNT(id), `Tag` from `images-tags`
    GROUP BY `Tag`
    ORDER BY 1 DESC
    LIMIT 20
    

    1, since it's the first column you want to group on.

提交回复
热议问题