I have a query similar to the example below (only column names changed for this post), that returns a paged selection of records from a MySql database using LIMIT, plus the count of records that would have been returned were the limit not applied via FOUND_ROWS().
In the case below SELECT FOUND_ROWS() simply returns 10, although I know that the query returns more rows if the limit is removed. I also have this working as I would expect for many several other very similar queries.
What could cause MySql to return an unexpected value that might explain what I’m seeing here?
SELECT
id,
firstname,
surname,
'' as anothercolumn1,
date as anothercolumn2,
'literal' as db
FROM some_table
WHERE
filter_col LIKE @val1
and surname LIKE @val2
ORDER BY surname, firstname
LIMIT 0, 10;
SELECT FOUND_ROWS();
You must add
SQL_CALC_FOUND_ROWSafter the 1stSELECTkeyword in order to get accurate result when you callFOUNC_ROWS():Manual