获取在MySQL中使用LIMIT时的总行数?
要获得使用LIMIT时的总行数,请使用以下语法-
select SQL_CALC_FOUND_ROWS * FROM yourTableName LIMIT 0,yourLastValue;
为了理解上述语法,让我们创建一个表。创建表的查询如下-
mysql> create table RowsUsingLimit -> ( -> Id int NOT NULL, -> Name varchar(10) -> );
现在,您可以使用insert命令在表中插入一些记录。查询如下-
mysql> insert into RowsUsingLimit values(10,'Larry'); mysql> insert into RowsUsingLimit values(9,'Mike'); mysql> insert into RowsUsingLimit values(15,'Sam'); mysql> insert into RowsUsingLimit values(20,'Bob'); mysql> insert into RowsUsingLimit values(1,'Carol'); mysql> insert into RowsUsingLimit values(18,'David');
使用select语句显示表中的所有记录。查询如下-
mysql> select *from RowsUsingLimit;
以下是输出-
+----+-------+ | Id | Name | +----+-------+ | 10 | Larry | | 9 | Mike | | 15 | Sam | | 20 | Bob | | 1 | Carol | | 18 | David | +----+-------+ 6 rows in set (0.00 sec)
这是查询以获取使用限制时的总行数-
mysql> select SQL_CALC_FOUND_ROWS * FROM RowsUsingLimit LIMIT 0,6;
以下是输出-
+----+-------+ | Id | Name | +----+-------+ | 10 | Larry | | 9 | Mike | | 15 | Sam | | 20 | Bob | | 1 | Carol | | 18 | David | +----+-------+ 6 rows in set (0.00 sec)