Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

COUNT with LIMIT in mysql query

Tags:

mysql

count

limit

i need to get total amount of rows when using LIMIT with my query to avoid twice querying. is it possible?

like image 278
hd. Avatar asked Nov 01 '10 10:11

hd.


1 Answers

Use FOUND_ROWS():

For a SELECT with a LIMIT clause, the number of rows that would be returned were there no LIMIT clause

use the statement right after your SELECT query, which needs the CALC_FOUND_ROWS keyword. Example from the manual:

SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name
WHERE id > 100 LIMIT 10;

Note that this puts additional strain on the database, because it has to find out the size of the full result set every time. Use SQL_CALC_FOUND_ROWS only when you need it.

like image 130
Pekka Avatar answered Nov 07 '22 13:11

Pekka