如何在MySQL中的集合函数COUNT()中包含NULL值?

问题描述:

按属性A3聚合查询组,然后执行COUNT(A4),但它不考虑属性A4中的NULL值。如何在MySQL中的集合函数COUNT()中包含NULL值?

I want the COUNT function to include counts of NULL values in attribute A4

对于常规的数量,不包括列名:

count(*) 

对于数不同,只需要添加额外的价值回:

count(distinct a4) + (case when count(a4) <> count(*) then 1 else 0 end) 

这可以在MySQL中简化为:

count(distinct a4) + (count(a4) <> count(*)) 

或者,如果您知道列中不存在价值:

count(distinct coalesce(a4, ' <NULL>'))