I have a function which needs the number of rows returned by a select. After some googling i found the FOUND_ROWS() function. But i don't think it works:
SELECT * FROM tipfirme LIMIT 20;
SELECT FOUND_ROWS();
because it always returns 1 as the found value.
However, if i run it in one line, it works.
SELECT FOUND_ROWS() FROM (SELECT * FROM tipfirme LIMIT 20) as T
Am i doing something wrong or is the function broken?
CodePudding user response:
FOUND_ROWS returns the number of rows the previous request (entire select statement) returned. It sounds to me like you are wanting just:
select count(1) from (select * From tipfirme limit 20) as T
select found_rows();
separately would not always return 1; I suspect you were not testing what you meant to test. If it immediately follows select * from tipfirme limit 20;
it would indeed return the number of rows the select returned (after the limit, or before the limit if you specified sql_calc_found_rows
in the previous select).
SELECT FOUND_ROWS() FROM (SELECT * FROM tipfirme LIMIT 20) as T
isn't doing what you think; it will return as many rows as the subselect returned, and each will have the number of rows the previously executed select returned, not related to the number of rows from the subselect at all.