当我们使用MySQLCOUNT()
函数对存储在还存储一些NULL值的列中的值进行计数时,MySQL将忽略NULL并仅返回非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)
现在,以下查询将COUNT()
方法应用于“工资”列-
mysql> Select COUNT(salary) from employee568; +---------------+ | COUNT(salary) | +---------------+ | 6 | +---------------+ 1 row in set (0.15 sec)
从上面的结果集中,很明显MySQL会忽略NULL并仅返回非NULL值的计数。