在 MySQL 中使用 LIMIT 时获取总行数?
mysqlmysqli database
要使用 LIMIT 时获取总行数,请使用以下语法x −
select SQL_CALC_FOUND_ROWS * FROM yourTableName LIMIT 0,yourLastValue;
为了理解上述语法,让我们创建一个表。创建表的查询如下 −
mysql> create table RowsUsingLimit -> ( -> Id int NOT NULL, -> Name varchar(10) -> ); Query OK, 0 rows affected (3.50 sec)
现在,您可以使用 insert 命令在表中插入一些记录。查询如下 −
mysql> insert into RowsUsingLimit values(10,'Larry'); Query OK, 1 row affected (0.17 sec) mysql> insert into RowsUsingLimit values(9,'Mike'); Query OK, 1 row affected (0.19 sec) mysql> insert into RowsUsingLimit values(15,'Sam'); Query OK, 1 row affected (0.18 sec) mysql> insert into RowsUsingLimit values(20,'Bob'); Query OK, 1 row affected (0.17 sec) mysql> insert into RowsUsingLimit values(1,'Carol'); Query OK, 1 row affected (0.14 sec) mysql> insert into RowsUsingLimit values(18,'David'); Query OK, 1 row affected (0.13 sec)
使用 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)