In mysql page how to get total records when the limit is applied?

3

Currently I'm doing two queries in my pages, in an extra, the list of data according to the page:

SELECT * FROM tabla LIMIT 20,10

In another I apply the COUNT to obtain how many records in total that query has:

SELECT COUNT(*) FROM tabla

Is there a way to not make two queries and get the number of total records in the same table of LIMIT no matter what happens again? Or some method to optimize that query? I work with PHP .

    
asked by CRUZ VARGAS 28.11.2018 в 18:47
source

1 answer

3

You can use SQL_CALC_FOUND_ROWS like this:

SELECT SQL_CALC_FOUND_ROWS * FROM tabla LIMIT 20,10

And then to get the value you want, you should run this query:

SELECT FOUND_ROWS();
    
answered by 28.11.2018 / 19:16
source