Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2023-09-10 20:41:08
forward
876 people have browsed it

如果列中还存储了一些 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)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!