当MySQLSUM()
函数与没有返回匹配行的SELECT语句一起使用时,则没有要评估的内容,并且它返回NULL作为输出。有时,我们认为它必须返回0作为输出,但是0本身就是一个数字,并且对于没有匹配的行,返回0并不重要,因此它返回NULL。为了理解上述概念,请考虑一个“ employee_tbl”表,该表具有以下记录-
mysql> SELECT * FROM employee_tbl; +------+------+------------+--------------------+ | id | name | work_date | daily_typing_pages | +------+------+------------+--------------------+ | 1 | John | 2007-01-24 | 250 | | 2 | Ram | 2007-05-27 | 220 | | 3 | Jack | 2007-05-06 | 170 | | 3 | Jack | 2007-04-06 | 100 | | 4 | Jill | 2007-04-06 | 220 | | 5 | Zara | 2007-06-06 | 300 | | 5 | Zara | 2007-02-06 | 350 | +------+------+------------+--------------------+ 7 rows in set (0.00 sec)
现在,SUM()
当我们运行以下查询来查找由“ Mohan”键入的页面总数时,MySQL函数返回NULL,该名称不在“名称”列中-
mysql> SELECT SUM(daily_typing_pages) FROM employee_tbl WHERE Name = ‘Mohan’; +-------------------------+ | SUM(daily_typing_pages) | +-------------------------+ | NULL | +-------------------------+ 1 row in set (0.00 sec)