MariaDB Count()函式


在MariaDB資料庫中,COUNT()函式用於返回表示式的計數/行數。

語法:

SELECT COUNT(aggregate_expression)  
FROM tables  
[WHERE conditions];

註:COUNT()函式只計算NOT NULL值。

範例:

假設有一個students表,有以下資料:

MariaDB [testdb]> select * from students;
+------------+--------------+-----------------+----------------+
| student_id | student_name | student_address | admission_date |
+------------+--------------+-----------------+----------------+
|          1 | Maxsu        | Haikou          | 2017-01-07     |
|          3 | JMaster      | Beijing         | 2016-05-07     |
|          4 | Mahesh       | Guangzhou       | 2016-06-07     |
|          5 | Kobe         | Shanghai        | 2016-02-07     |
|          6 | Blaba        | Shengzhen       | 2016-08-07     |
|          7 | Maxsu        | Sanya           | 2017-08-08     |
|          8 | Maxsu        | Haikou          | 2015-11-17     |
+------------+--------------+-----------------+----------------+
7 rows in set (0.00 sec)

students表中統計student_id

SELECT COUNT(student_id) FROM Students;
-- 或者
SELECT COUNT(*) FROM Students;

執行上面查詢語句,得到以下結果 -

MariaDB [testdb]> SELECT COUNT(student_id) FROM Students;
+-------------------+
| COUNT(student_id) |
+-------------------+
|                 7 |
+-------------------+
1 row in set (0.07 sec)

1. COUNT()函式與單一表示式

統計student_nameMaxsuKobe的學生人數。參考以下查詢語句 -

SELECT COUNT(*) AS "Number of Students"  
FROM Students  
WHERE student_name in ('Maxsu', 'Kobe');

執行上面查詢語句,得到以下結果 -

MariaDB [testdb]> SELECT COUNT(*) AS "Number of Students"
    -> FROM Students
    -> WHERE student_name in ('Maxsu', 'Kobe');
+--------------------+
| Number of Students |
+--------------------+
|                  4 |
+--------------------+
1 row in set (0.00 sec)

2. COUNT()函式與Distinct子句

DISTINCT子句與COUNT()函式一起使用以防止重複計數。它只包含原始記錄。

SELECT COUNT(DISTINCT student_name) AS "Number of Unique names"  
FROM Students  
WHERE student_name in ('Maxsu', 'Kobe');

執行上面查詢語句,得到以下結果 -

MariaDB [testdb]> SELECT COUNT(DISTINCT student_name) AS "Number of Unique names"
    -> FROM Students
    -> WHERE student_name in ('Maxsu', 'Kobe');
+------------------------+
| Number of Unique names |
+------------------------+
|                      2 |
+------------------------+
1 row in set (0.08 sec)

從查詢結果中可以看到,比上一個範例少了兩行。

3. COUNT()函式與NULL值

為了更好地演示COUNT()函式對NULL值的處理,這裡再插入兩條記錄 -

-- 修改表欄位接受NULL預設值
ALTER TABLE students CHANGE student_address student_address varchar(32) default NULL;
-- 插入第1行
INSERT INTO students  
(student_name, student_address, admission_date)  
VALUES('Himin',NULL,'2017-01-07 00:00:00');

-- 插入第2行
INSERT INTO students  
(student_name, student_address, admission_date)  
VALUES('Hiavg',NULL,NULL);

執行上面查詢語句,得到以下結果 -

MariaDB [testdb]> select * from students;
+------------+--------------+-----------------+----------------+
| student_id | student_name | student_address | admission_date |
+------------+--------------+-----------------+----------------+
|          1 | Maxsu        | Haikou          | 2017-01-07     |
|          3 | JMaster      | Beijing         | 2016-05-07     |
|          4 | Mahesh       | Guangzhou       | 2016-06-07     |
|          5 | Kobe         | Shanghai        | 2016-02-07     |
|          6 | Blaba        | Shengzhen       | 2016-08-07     |
|          7 | Maxsu        | Sanya           | 2017-08-08     |
|          8 | Maxsu        | Haikou          | 2015-11-17     |
|          9 | Himin        | NULL            | 2017-01-07     |
|         10 | Hiavg        | NULL            | NULL           |
+------------+--------------+-----------------+----------------+
9 rows in set (0.00 sec)

現在來看看使用count()函式來測試對NULL值的計算效果。

select count(student_address) from students;

執行上面查詢語句,得到以下結果 -

MariaDB [testdb]> select count(student_address) from students;
+------------------------+
| count(student_address) |
+------------------------+
|                      7 |
+------------------------+
1 row in set (0.00 sec)

可以看到,COUNT(student_address)函式它並沒有統計包含NULL值的行。