发出获取COUNT以将NULL值返回为0的问题

时间:2015-04-09 16:58:55

标签: mysql ms-access count null

我需要一个Count,当它发现没有任何内容可以输入报告时返回0。

我发现一个资源说我需要使用MS Access'NZ函数,但它似乎不起作用。它只是一个表,所以连接不是问题。

SELECT tbl_Supporting.Ref, tbl_Supporting.Application, tbl_Supporting.Level, NZ(Count(tbl_Supporting.Level),0) AS CountOfLevel34
FROM tbl_Supporting
GROUP BY tbl_Supporting.Ref, tbl_Supporting.Application, tbl_Supporting.Level
WHERE (((tbl_Supporting.Level)=3)) OR (((tbl_Supporting.Level)=4));

似乎返回与

完全相同的东西
SELECT tbl_Supporting.Ref, tbl_Supporting.Application, tbl_Supporting.Level, Count(tbl_Supporting.Level AS CountOfLevel34
FROM tbl_Supporting
GROUP BY tbl_Supporting.Ref, tbl_Supporting.Application, tbl_Supporting.Level
WHERE (((tbl_Supporting.Level)=3)) OR (((tbl_Supporting.Level)=4));

有人可以告诉我为什么会这样吗?它肯定会返回NULL值。

2 个答案:

答案 0 :(得分:0)

使用IFNULL功能。即IFNULL(计数(字段),0)

https://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_ifnull

编辑:如果问题出在MS Access中,那么将NZ功能移到计数参数中:

计数(NZ(tbl_Supporting.Level),0)

答案 1 :(得分:0)

正如你所说:

WHERE tbl_Supporting.Level=3 

你不会检索,更不用说计算任何Level为Null的记录。

所以Nz不能在这里添加任何东西:

NZ(Count(tbl_Supporting.Level),0)

您必须修改标准。

好的,那么你可能需要Sum而不是Count:

SELECT tbl_Supporting.Ref, tbl_Supporting.Application, tbl_Supporting.Level, Sum(Int(tbl_Supporting.Level / 3)) AS CountOfLevel34
FROM tbl_Supporting
GROUP BY tbl_Supporting.Ref, tbl_Supporting.Application, tbl_Supporting.Level
WHERE (((tbl_Supporting.Level)=3)) OR (((tbl_Supporting.Level)=4));