如何在不使用 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)

更新於: 30-7-2019

640 次瀏覽

開啟你的 職業

完成課程並獲得認證

開始學習
廣告
© . All rights reserved.