Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - How to count rows before pagination?

I am making a search page to find users. I have que query to find them and actually I can do the pagination with "LIMIT startRow, numberRows". But how could I count the total number of "registers" found before doing the pagination? I would like to add at my search page, the number of the users found in a search.

I need something like: "Page 1 of 100". I actually I have "Page 1" but I don't know how to count the total of results before paginate.

¿Maybe could be necesary execute an extra query with "SELECT COUNT(*)"? ¿Is there another way to count before pagination to avoid another query?

I use two sql queries, one for single word, and another for multiword:

Base sql query (for single word and multi word search):

"SELECT * FROM accounts AS A INNER JOIN profiles AS P ON A.account_id = P.account_id "

Single word condition:

"WHERE A.username LIKE ? OR P.name LIKE ? OR P.name LIKE ? OR P.surname LIKE ? OR P.surname LIKE ? LIMIT ?,?"

Multi word condition:

"WHERE CONCAT(P.name, ' ', P.surname) LIKE ? LIMIT ?,?"

Thanks a lot.

like image 782
wilmerlpr Avatar asked Jan 02 '16 20:01

wilmerlpr


People also ask

How do I count specific rows in MySQL?

To counts all of the rows in a table, whether they contain NULL values or not, use COUNT(*). That form of the COUNT() function basically returns the number of rows in a result set returned by a SELECT statement.

How do I count the number of rows returned by a query in MySQL?

The COUNT(*) function returns the number of rows in a result set returned by a SELECT statement. The COUNT(*) returns the number of rows including duplicate, non-NULL and NULL rows.

How do you get the total records even if limit is applied?

Since MYSQL 4.0 we can use SQL_CALC_FOUND_ROWS option in query which will tell MySQL to count total number of rows disregarding LIMIT clause. In main query add SQL_CALC_FOUND_ROWS option just after SELECT and in second query use FOUND_ROWS() function to get total number of rows without executing the query.

How do I count rows in MySQL by group?

In MySQL, the COUNT() function calculates the number of results from a table when executing a SELECT statement. It does not contain NULL values. The function returns a BIGINT value. It can count all the matched rows or only rows that match the specified conditions.


1 Answers

Modify the query like this:

SELECT SQL_CALC_FOUND_ROWS * FROM accounts ... LIMIT ...

This will return the same limited/offset result as before (the first page of results, for example), but if you then immediately send this second query to the server...

SELECT FOUND_ROWS();

The server will return a count of the total number of rows that would have been returned if the LIMIT had not been imposed on the previous query. There's your total number of results.

This will, of course, mean that your initial query takes longer, because the optimizer can't take any shortcuts that might allow it to stop evaluating rows once the LIMIT is satisfied, but in some cases, no such shortcuts are available anyway.

This is the "official" mechanism for doing what you are trying to accomplish.

http://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_found-rows

like image 149
Michael - sqlbot Avatar answered Oct 10 '22 01:10

Michael - sqlbot