如何在不使用 count(*) MySQL 查询的情况下获得表中的行数?
可以使用 count(1)。我们先看语法 -
select count(1) from yourTableName;
我们先创建一个表 -
mysql> create table DemoTable ( StudentName varchar(100) ); Query OK, 0 rows affected (0.84 sec)
使用插入命令在表中插入一些记录 -
mysql> insert into DemoTable(StudentName) values('John Smith'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable(StudentName) values('Chris Brown'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentName) values('David Miller'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(StudentName) values('Carol Taylor'); Query OK, 1 row affected (0.15 sec)
使用 select 语句显示表中的所有记录 -
mysql> select *from DemoTable;
输出
+--------------+ | StudentName | +--------------+ | John Smith | | Chris Brown | | David Miller | | Carol Taylor | +--------------+ 4 rows in set (0.00 sec)
以下查询不使用 count(*) 获取表中的行数 -
mysql> select count(1) from DemoTable;
输出
+----------+ | count(1) | +----------+ | 4 | +----------+ 1 row in set (0.03 sec)
广告