Skip to main content
Version: V1.0.0

COUNT

Declaration

COUNT([DISTINCT | ALL] expr)

Description

Returns the number of non-NULL values in the rows retrieved by the SELECT statement. If no matching rows are found, it returns 0. The DISTINCT option can be used to return the number of distinct values of expr.

The difference between COUNT(*) and COUNT(expr) is that COUNT(*) returns the number of retrieved rows, regardless of whether they contain NULL values.

Examples

SELECT * FROM test;
+----+------+------+
| id | ip | ip2 |
+----+------+------+
| 1 | 4 | NULL |
| 3 | 3 | NULL |
| 4 | 3 | NULL |
+----+------+------+
3 rows in set (0.002 sec)

SELECT COUNT(ip2), COUNT(ip), COUNT(DISTINCT(ip)), COUNT(*) FROM test;
+------------+-----------+---------------------+----------+
| count(ip2) | count(ip) | count(distinct(ip)) | count(*) |
+------------+-----------+---------------------+----------+
| 0 | 3 | 2 | 3 |
+------------+-----------+---------------------+----------+
1 row in set (0.001 sec)