MySQL count function

MySQL count function

The COUNT() function returns the number of rows that matches a specified criteria.

COUNT function to count the number rows in a table.

The return type of the COUNT function is BIGINT. The COUNT function will return zero if there was no matching row found.

The COUNT(*) function returns the number of rows in a result set returned by a SELECT statement. The COUNT(*) function counts rows that contain non-NULL and NULL values.

If you use the COUNT(*) function to count the rows in a table without using a WHERE clause and selecting other columns, it will perform very fast.

 

 

COUNT() Syntax

SELECT COUNT(column_name)
FROM table_name
WHERE condition;

Query example:

SELECT COUNT(IF(status=‘Cancelled’,1, NULL)) ‘Cancelled’,
       COUNT(IF(status=‘On Hold’,1, NULL)) ‘On Hold’,
       COUNT(IF(status=‘Disputed’,1, NULL)) ‘Disputed’
FROM orders;
SELECT productvendor, count(*)
FROM products
GROUP BY productvendor
HAVING count(*) >= 9;
SELECT productvendor, count(*)
FROM products
GROUP BY productvendor;
SELECT COUNT(ProductID)
FROM Products;