如果count(*)为NULL,则返回零

时间:2010-12-07 17:46:22

标签: mysql

如果count(*)为NULL,如何修改查询以返回0?

select monthname(timestamp), year(timestamp), count(*) 
from largehits 
where largeID = someid 
    and (month(timestamp)<=somemonth 
    and month(timestamp)>=somemonth 
    and year(timestamp)<=someyear 
    and year(timestamp)>=someyear ) 
group by month(timestamp), year(timestamp) 
order by timestamp asc

谢谢!

1 个答案:

答案 0 :(得分:2)

BoltClock在COUNT永远不会NULL的评论中提出了一个很好的观点。

通常,您只会使用COALESCE(expression,0)来解决此类问题。

修改:在评论中进行说明后,您需要在数字表格上使用outer join来获得所需的效果。