Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this possible to get total number of rows count with offset limit

Hey Guyz Is this possible to get total number of rows count with offset limit

Scenario

SELECT * FROM users limit 0,5; 

This Query contain 300 records but the issue is if i call this query with offset the result will be show only 5 record and i don't want to write a Query in twice time. one for paging limit and other for total no of record count...

I don't want this

SELECT * FROM users limit 0,5; // paging 
SELECT count(*) FROM users; // count 

i have to merge this Queries or helps are definitely appreciated

like image 919
Query Master Avatar asked Apr 10 '12 06:04

Query Master


1 Answers

You can use SQL_CALC_FOUND_ROWS like this

SELECT SQL_CALC_FOUND_ROWS * FROM users limit 0,5;

It gets the row count before applying any LIMIT clause. It does need another query to fetch the results but that query can simply be

SELECT FOUND_ROWS()

and hence you don't have to repeat your complicated query.

like image 147
liquorvicar Avatar answered Sep 21 '22 12:09

liquorvicar