Skip to main content

COUNT

Description​

Returns the number of non-NULL records in the specified column, or the total number of records.

Syntax​

COUNT(DISTINCT <expr> [,<expr>,...])
COUNT(*)
COUNT(<expr>)

Parameters​

ParameterDescription
<expr>Conditional expression (column name)

Return Value​

The return value is of numeric type. If expr is NULL, there will be no parameter statistics.

Example​

select * from test_count;
+------+------+------+
| id | name | sex |
+------+------+------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 1 |
| 4 | 0 | 1 |
| 4 | 4 | 1 |
| 5 | NULL | 1 |
+------+------+------+
select count(*) from test_count;
+----------+
| count(*) |
+----------+
| 6 |
+----------+
select count(name) from test_insert;
+-------------+
| count(name) |
+-------------+
| 5 |
+-------------+
select count(distinct sex) from test_insert;
+---------------------+
| count(DISTINCT sex) |
+---------------------+
| 1 |
+---------------------+
select count(distinct id,sex) from test_insert;
+-------------------------+
| count(DISTINCT id, sex) |
+-------------------------+
| 5 |
+-------------------------+