假设如果我们正在计算同样具有NULL值的列的值的总和,那么MySQLSUM()
函数将忽略NULL值,并进行其余值的总和。要理解它,请请看以下表“ employee”的示例,其中包含以下详细信息-
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)
现在,假设如果要从上表中获取员工的总薪水,那么在使用SUM()
函数进行计算时,它将忽略NULL值。以下查询将产生所需的结果集-
mysql> Select SUM(Salary) from Employee; +-------------+ | SUM(Salary) | +-------------+ | 210000 | +-------------+ 1 row in set (0.00 sec)
也可以在以下查询的帮助下进行验证-
mysql> Select SUM(Salary) from Employee WHERE Salary IS NOT NULL; +-------------+ | SUM(Salary) | +-------------+ | 210000 | +-------------+ 1 row in set (0.00 sec)