Bug #53078 | SELECT WITH LIMIT PROBLEM | ||
---|---|---|---|
Submitted: | 22 Apr 2010 19:03 | Modified: | 28 Apr 2010 20:32 |
Reporter: | jonathan touchette | Email Updates: | |
Status: | Not a Bug | Impact on me: | |
Category: | MySQL Server: General | Severity: | S2 (Serious) |
Version: | 5.1.45-community | OS: | Windows |
Assigned to: | CPU Architecture: | Any | |
Tags: | bug, count, limit, problem, SELECT |
[22 Apr 2010 19:03]
jonathan touchette
[23 Apr 2010 3:36]
Valeriy Kravchuk
Sorry, but LIMIT is applied last, to the final result set, even after HAVING and ORDER BY clauses. This is documented (see http://dev.mysql.com/doc/refman/5.1/en/select.html) and nobody is going to change this. You have to use some other, more creative SQL instead of: mysql> select count(*) from mysql.user limit 1; +----------+ | count(*) | +----------+ | 9 | +----------+ 1 row in set (0.00 sec) Like these maybe: mysql> select 1 from mysql.user having count(*) > 0; +---+ | 1 | +---+ | 1 | +---+ 1 row in set (0.00 sec) mysql> select 1 from mysql.user having count(*) >= 5; +---+ | 1 | +---+ | 1 | +---+ 1 row in set (0.00 sec) mysql> select 1 from mysql.user having count(*) >= 10; Empty set (0.00 sec) Statements above answers the questions like: "Are there more than N rows in the table?"
[28 Apr 2010 20:32]
jonathan touchette
SOLUTION: SELECT * FROM `MY TABLE` [WHERE field = value ...] LIMIT 5; SELECT FOUND_ROWS(); /*If there is more than 5 rows, it will return 5 otherwise, it will return the number of rows */