Select*fromEmployee;+----+--------+--------+|ID |Name |Salary|+----+--------+--------+|1 |Gaurav|50000 ||2&nb"/> Select*fromEmployee;+----+--------+--------+|ID |Name |Salary|+----+--------+--------+|1 |Gaurav|50000 ||2&nb">

Home  >  Article  >  Database  >  What does the MySQL COUNT() function return if there are also some NULL values ​​stored in the column?

What does the MySQL COUNT() function return if there are also some NULL values ​​stored in the column?

WBOY
WBOYforward
2023-09-10 20:41:08869browse

如果列中还存储了一些 NULL 值,MySQL COUNT() 函数会返回什么?

When we use the MySQL COUNT() function to count the values ​​stored in a column that also stores some NULL values, MySQL ignores NULLs and returns only non-NULLs value result. To understand it, we use data from table "Employee" as shown below -

mysql> Select * from Employee;
+----+--------+--------+
| ID | Name   | Salary |
+----+--------+--------+
| 1  | Gaurav | 50000  |
| 2  | Rahul  | 20000  |
| 3  | Advik  | 25000  |
| 4  | Aarav  | 65000  |
| 5  | Ram    | 20000  |
| 6  | Mohan  | 30000  |
| 7  | Aryan  | NULL   |
| 8  | Vinay  | NULL   |
+----+--------+--------+
8 rows in set (0.00 sec)

Now, the following query applies COUNT() function on "Salary" column -

mysql> Select COUNT(salary) from employee568;
+---------------+
| COUNT(salary) |
+---------------+
| 6             |
+---------------+
1 row in set (0.15 sec)

From above As can be clearly seen in the result set, MySQL ignores NULL and only returns the count of non-NULL values.

The above is the detailed content of What does the MySQL COUNT() function return if there are also some NULL values ​​stored in the column?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete