COUNT与LIMIT在mysql查询 [英] COUNT with LIMIT in mysql query
问题描述
我需要获取总量的行使用LIMIT与我的查询,以避免两次查询。
是可能吗?
i need to get total amount of rows when using LIMIT with my query to avoid twice querying. is it possible?
推荐答案
使用 FOUND_ROWS()
:
对于具有LIMIT子句的SELECT,返回的行数没有LIMIT子句
For a SELECT with a LIMIT clause, the number of rows that would be returned were there no LIMIT clause
在SELECT查询之后,需要 CALC_FOUND_ROWS
关键字。示例从手册:
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;
请注意,这会给数据库带来额外的压力,因为它必须找出完整的结果集每次。只有在需要时才使用 SQL_CALC_FOUND_ROWS
。
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.
这篇关于COUNT与LIMIT在mysql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!