如何在不使用 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)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP