0

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?

1 Answers1

1

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.

ysth
  • 96,171
  • 6
  • 121
  • 214
  • 1
    It doesn't work with update queries. For those you use `ROW_COUNT()`. – Barmar Feb 07 '22 at 21:28
  • @Barmar thanks . – ysth Feb 07 '22 at 21:29
  • thanks for the "select count(1) from (select * From tipfirme limit 20) as T". it works and i should've thought about it... to make it more clear: i just ran those to lines of sql. nothing more, nothing less, from the SQL window of phpmyadmin. I'm using mariadb stored on a synology nas – Adelina Andreea trandafir Feb 08 '22 at 11:03