开发者

Return Zero if count(*) is NULL

开发者 https://www.devze.com 2023-01-29 13:07 出处:网络
How can I modify my query to return 0 if count(*) is NULL? select monthname(timestamp), year(timestamp), count(*)

How can I modify my query to return 0 if count(*) is NULL?

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

Thanks!


BoltClock makes a good point in the comments that COUNT can never be NULL.

Generally you would just use COALESCE(expression,0) for this type of problem though.

Edit: Following clarification in the comments you would need to use an outer join on a numbers table to get the effect that you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消