Select*fromEmployee;+----+--------+--------+ |ID|Name|Salary|+----+--------+--------+|1|Gaurav|50000||2|Rahul|20000||3|Advik| 25000||4|Aarav|65000||5|Ra"/> Select*fromEmployee;+----+--------+--------+ |ID|Name|Salary|+----+--------+--------+|1|Gaurav|50000||2|Rahul|20000||3|Advik| 25000||4|Aarav|65000||5|Ra">

Home  >  Article  >  Database  >  How does the MySQL SUM() function evaluate whether a column also has a NULL value?

How does the MySQL SUM() function evaluate whether a column also has a NULL value?

PHPz
PHPzforward
2023-09-14 08:09:06911browse

MySQL SUM() 函数如何评估列是否也有 NULL 值?

Suppose we are calculating the sum of the values ​​of a column that also has NULL values, then the MySQL SUM() function ignores the NULL values ​​and calculates the sum of the remaining values. To understand it, consider the following example of table "employee" which contains the following details -

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, suppose we want to get the total salary of the employees in the above table, then while calculating it using SUM() function, it ignores NULL values. The following query will produce the desired result set -

mysql> Select SUM(Salary) from Employee;
+-------------+
| SUM(Salary) |
+-------------+
| 210000      |
+-------------+
1 row in set (0.00 sec)

You can also verify it with the help of the following query -

mysql> Select SUM(Salary) from Employee WHERE Salary IS NOT NULL;
+-------------+
| SUM(Salary) |
+-------------+
|   210000    |
+-------------+
1 row in set (0.00 sec)

The above is the detailed content of How does the MySQL SUM() function evaluate whether a column also has a NULL value?. 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