如果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
谢谢!
答案 0 :(得分:2)
BoltClock在COUNT
永远不会NULL
的评论中提出了一个很好的观点。
通常,您只会使用COALESCE(expression,0)
来解决此类问题。
修改:在评论中进行说明后,您需要在数字表格上使用outer join
来获得所需的效果。