MySQL 4.1中新增了FOUND_ROWS()函数,关于这个函数的说明如下:
For a SELECT with a LIMIT clause, the number of rows that would bereturned were there no LIMIT clauseA SELECT statement may include a LIMIT clause to restrict thenumber of rows the server returns to the client. In some cases, itis desirable to know how many rows the statement would havereturned without the LIMIT, but without running the statementagain. To obtain this row count, include a SQL_CALC_FOUND_ROWSoption in the SELECT statement, and then invoke FOUND_ROWS()afterward:
例如需要取出一张表的前10行,同时又需要取出符合条件的总数。这在某些翻页操作中很常见SELECT SQL_CALC_FOUND_ROWS * FROM your_table_name
WHERE id > 33 LIMIT 10;
在上一查询之后,你只需要用FOUND_ROWS()就能获得查询总数,这个数目是抛掉了LIMIT之后的结果数:
SELECT FOUND_ROWS();
其中第一个sql里面的SQL_CALC_FOUND_ROWS不可省略,它表示需要取得结果数,也是后面使用FOUND_ROWS()函数的铺垫。注意:FOUND_ROWS()返回的结果是临时的。如果程序往后会用到这个数字,必须提前它保存在一个变量中待用。
FOUND_ROWS()与count()的 区别:
1、当SQL限制条件太多时, count()的执行效率不是很高,最好使用FOUND_ROWS()2、当SQL查询语句没有where等条件限制时,使用count()函数的执行效率较高。