COUNT
更新时间:2025-10-16
描述
返回指定列的非 NULL 记录数,或者记录总数
语法
SQL
1COUNT(DISTINCT <expr> [,<expr>,...])
2COUNT(*)
3COUNT(<expr>)
参数
| 参数 | 说明 |
|---|---|
<expr> |
条件表达式(列名) |
返回值
返回值为数值类型。如果 expr 为 NULL,则不参数统计
举例
SQL
1select * from test_count;
Text
1+------+------+------+
2| id | name | sex |
3+------+------+------+
4| 1 | 1 | 1 |
5| 2 | 2 | 1 |
6| 3 | 3 | 1 |
7| 4 | 0 | 1 |
8| 4 | 4 | 1 |
9| 5 | NULL | 1 |
10+------+------+------+
SQL
1select count(*) from test_count;
Text
1+----------+
2| count(*) |
3+----------+
4| 6 |
5+----------+
SQL
1select count(name) from test_insert;
Text
1+-------------+
2| count(name) |
3+-------------+
4| 5 |
5+-------------+
SQL
1select count(distinct sex) from test_insert;
Text
1+---------------------+
2| count(DISTINCT sex) |
3+---------------------+
4| 1 |
5+---------------------+
SQL
1select count(distinct id,sex) from test_insert;
Text
1+-------------------------+
2| count(DISTINCT id, sex) |
3+-------------------------+
4| 5 |
5+-------------------------+
