如何在此查询中防止被零除?

时间:2019-01-28 15:48:01

标签: php mysql

如何在以下查询中防止被零除?

TRUNCATE(abs((((sum(date(timestamp) = curdate()) - sum((timestamp BETWEEN concat(date(date_add(now(), interval -1 day)), ' 00:00:00')
AND 
date_sub(now( ), interval 24 hour)))) / ((sum(date(timestamp) = curdate()) + 
sum((timestamp BETWEEN concat(date(date_add(now(), interval -1 day)), ' 00:00:00')
AND 
date_sub(now( ), interval 24 hour))))/2))*100)),0) AS percentage_diff

1 个答案:

答案 0 :(得分:0)

如果在此示例中为0,我将分配为1

truncate(abs((((SUM(DATE(TIMESTAMP) = CURDATE()) - SUM((TIMESTAMP 
    between CONCAT(DATE(DATE_ADD(NOW(), INTERVAL -1 DAY)), ' 00:00:00') 
    and DATE_SUB(NOW( ), INTERVAL 24 HOUR)))) / 

     if (
          (( sum(date(timestamp) = curdate()) + 
               SUM((TIMESTAMP between CONCAT(DATE(DATE_ADD(NOW(), INTERVAL -1 DAY)), ' 00:00:00') 
               and DATE_SUB(NOW( ), INTERVAL 24 HOUR))))/2)  = 0, 1 ,
          ((sum(date(timestamp) = curdate()) + 
               SUM((TIMESTAMP between CONCAT(DATE(DATE_ADD(NOW(), INTERVAL -1 DAY)), ' 00:00:00') 
               and DATE_SUB(NOW( ), INTERVAL 24 HOUR))))/2) 
    )
    )*100)),0) as percentage_diff