Name

COUNT( )

Synopsis

COUNT([DISTINCT] column)

This function returns the number of rows retrieved in the SELECT statement for the column given. Rows in which the column given is NULL are not counted. If the wildcard * is used as the argument, the function counts all rows, including ones with NULL values. Use this in conjunction with the GROUP BY clause. The second syntax returns the number of unique non-NULL values found. This function is set to optimal performance for MyISAM tables if a WHERE clause is not included in a statement.

SELECT col1, COUNT(col2),
       COUNT(DISTINCT col2)
   FROM table1
   GROUP BY col1;

Get MySQL in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.