在mysql查询中使用LIMIT COUNT

时间:2021-02-26 22:19:15

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

在我的查询中使用LIMIT时,我需要获得总行数,以避免两次查询。可能吗?

1 个解决方案

#1


14  

Use FOUND_ROWS():

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

对于带有LIMIT子句的SELECT,返回的行数没有LIMIT子句

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

在SELECT查询之后立即使用该语句,该查询需要CALC_FOUND_ROWS关键字。手册中的示例:

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.

请注意,这会给数据库带来额外的压力,因为它必须每次都找出完整结果集的大小。仅在需要时使用SQL_CALC_FOUND_ROWS。

#1


14  

Use FOUND_ROWS():

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

对于带有LIMIT子句的SELECT,返回的行数没有LIMIT子句

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

在SELECT查询之后立即使用该语句,该查询需要CALC_FOUND_ROWS关键字。手册中的示例:

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.

请注意,这会给数据库带来额外的压力,因为它必须每次都找出完整结果集的大小。仅在需要时使用SQL_CALC_FOUND_ROWS。