MySql中一行的计算值为空时的情况

MySql中一行的计算值为空时的情况,mysql,case,ifnull,Mysql,Case,Ifnull,我有一个很长的MySql字符串,用于计算某个值的每日百分比。然而,如果某一天没有任何东西,它只会跳过这一天,转到下一天。我需要它为它通常跳过的一天吐出一个“0”。谢谢你的帮助 SELECT day(timestamp), CASE when round(count(w_comp_current_1+W_comp_current_2)*10/86400*100,1) as 'run_time2' iS NULL then '0' ELSE round(count(w_comp_current_

我有一个很长的MySql字符串,用于计算某个值的每日百分比。然而,如果某一天没有任何东西,它只会跳过这一天,转到下一天。我需要它为它通常跳过的一天吐出一个“0”。谢谢你的帮助

SELECT day(timestamp), CASE when
round(count(w_comp_current_1+W_comp_current_2)*10/86400*100,1) as 'run_time2' iS NULL 
then '0' 
ELSE round(count(w_comp_current_1+W_comp_current_2)*10/86400*100,1) as 'run_time2' END 
FROM location.db WHERE timestamp between subdate(curdate(), interval 1 month) 
and curdate() AND (w_comp_current_1+w_comp_current_2) > 45 
GROUP BY MONTH(Timestamp), DAY(Timestamp) 
ORDER BY Timestamp
使用日历表的新查询:

Select date_format(calendar.timestamp,'%b-%e') as 'Month-Day', round(count(w_comp_current_1+W_comp_current_2)*10/86400*100,1) as 'run_time2' from calendar 
Left Join courthouse on calendar.timestamp = courthouse.timestamp 
WHERE calendar.timestamp between subdate(curdate(), interval 1 month) and curdate() and calendar.timestamp > '2013-10-03%'  AND (w_comp_current_1+w_comp_current_2) > 45 
GROUP BY MONTH(calendar.Timestamp), DAY(calendar.Timestamp) ORDER BY calendar.Timestamp

案例列的别名有两次,两次都在错误的位置。只能在案件结束陈述后给出:

SELECT day(TIMESTAMP), CASE 
    WHEN round(count(w_comp_current_1 + W_comp_current_2) * 10 / 86400 * 100, 1) IS NULL 
      THEN '0'
    ELSE round(count(w_comp_current_1 + W_comp_current_2) * 10 / 86400 * 100, 1) 
    END AS 'run_time2' 
FROM location.db
WHERE TIMESTAMP BETWEEN subdate(curdate(), interval 1 month) AND curdate()
  AND (w_comp_current_1 + w_comp_current_2) > 45
GROUP BY MONTH(TIMESTAMP), DAY(TIMESTAMP)
ORDER BY TIMESTAMP

好啊这很有效。但在没有计算值的那一天,它仍然没有给我一个“0”。也许“if null”不是正确的方法?实际上,日期(时间戳)不需要受“(w_comp_current_1+w_comp_current_2)>45”where子句的限制。因为我希望每天都能出现,这完全是另一个问题。您必须使用类似于日历表的东西,以便查询返回日历表上没有记录的天数的值,并在stackoverflow中搜索这些值。你会发现很多例子来跟随你感谢你。所以解决这个问题的唯一方法是创建另一个表。这不是唯一的方法。这是单向的:p。例如,见其他问题。它有两个解决方案