SQL Group By with an Order By
In all versions of MySQL, simply alias the aggregate in the SELECT list, and order by the alias: SELECT COUNT(id) AS theCount, `Tag` from `images-tags` GROUP BY `Tag` ORDER BY theCount DESC LIMIT 20
In all versions of MySQL, simply alias the aggregate in the SELECT list, and order by the alias: SELECT COUNT(id) AS theCount, `Tag` from `images-tags` GROUP BY `Tag` ORDER BY theCount DESC LIMIT 20
You need to use HAVING, not WHERE. The difference is: the WHERE clause filters which rows MySQL selects. Then MySQL groups the rows together and aggregates the numbers for your COUNT function. HAVING is like WHERE, only it happens after the COUNT value has been computed, so it’ll work as you expect. Rewrite your subquery … Read more