I want to LIMIT Grouped results to 30, but instead I'm grouping all rows and then limiting it to 30 groups. How do I do the opposite?
SELECT *, COUNT(*) AS COUNT FROM `Posts` GROUP By `Category` LIMIT 30
The ORDER BY clause goes after the FROM clause but before the LIMIT .
No, you can't LIMIT subqueries arbitrarily (you can do it to a limited extent in newer MySQLs, but not for 5 results per group). This is a groupwise-maximum type query, which is not trivial to do in SQL.
When combining the Group By and Order By clauses, it is important to bear in mind that, in terms of placement within a SELECT statement: The GROUP BY clause is placed after the WHERE clause. The GROUP BY clause is placed before the ORDER BY clause.
The MySQL LIMIT ClauseThe LIMIT clause is used to specify the number of records to return. The LIMIT clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.
SELECT *, COUNT(*) AS COUNT FROM (SELECT * FROM `Posts` LIMIT 30) t GROUP By `Category`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With