最快的方法,来统计 MySQL 表中的行数?
让我们首先看一个关于创建表、添加记录并显示它们的示例。CREATE 命令用于创建表。
mysql> CREATE table RowCountDemo -> ( -> ID int, -> Name varchar(100) > ); Query OK, 0 rows affected (0.95 sec)
使用 INSERT 命令插入记录。
mysql>INSERT into RowCountDemo values(1,'Larry'); Query OK, 1 row affected (0.15 sec) mysql>INSERT into RowCountDemo values(2,'John'); Query OK, 1 row affected (0.13 sec) mysql>INSERT into RowCountDemo values(3,'Bela'); Query OK, 1 row affected (0.15 sec) mysql>INSERT into RowCountDemo values(4,'Jack'); Query OK, 1 row affected (0.11 sec) mysql>INSERT into RowCountDemo values(5,'Eric'); Query OK, 1 row affected (0.19 sec) mysql>INSERT into RowCountDemo values(6,'Rami'); Query OK, 1 row affected (0.49 sec) mysql>INSERT into RowCountDemo values(7,'Sam'); Query OK, 1 row affected (0.14 sec) mysql>INSERT into RowCountDemo values(8,'Maike'); Query OK, 1 row affected (0.77 sec) mysql>INSERT into RowCountDemo values(9,'Rocio'); Query OK, 1 row affected (0.13 sec) mysql>INSERT into RowCountDemo values(10,'Gavin'); Query OK, 1 row affected (0.19 sec)
显示记录。
mysql>SELECT *from RowCountDemo;
以下是上述查询的输出。
+------+-------+ | ID | Name | +------+-------+ | 1 | Larry | | 2 | John | | 3 | Bela | | 4 | Jack | | 5 | Eric | | 6 | Rami | | 7 | Sam | | 8 | Maike | | 9 | Rocio | | 10 | Gavin | +------+-------+ 10 rows in set (0.00 sec)
为了快速统计行数,我们有以下两个选项 −
查询 1
mysql >SELECT count(*) from RowCountDemo;
以下是上述查询的输出。
+----------+ | count(*) | +----------+ | 10 | +----------+ 1 row in set (0.00 sec)
查询 2
mysql>SELECT count(found_rows()) from RowCountDemo;
以下是上述查询的输出。
+---------------------+ | count(found_rows()) | +---------------------+ | 10 | +---------------------+ 1 row in set (0.00 sec)
广告