MySQL COUNT在明天的日期基于CURDATE

时间:2014-12-19 20:34:07

标签: mysql sql

通过计算大于MYSQL COUNT的记录,我的CURDATE状态效果很好。如何根据指定列的CURDATE明确指定此查询?

SELECT COUNT(ID) as total_count 
FROM HTG_ScheduleRequest 
WHERE (ScheduleDateCurrent > CURDATE()) 
    AND JobStatus = '3' 
GROUP BY SSR

3 个答案:

答案 0 :(得分:1)

以下查询可以解决问题

SELECT COUNT(ID) as total_count 
FROM HTG_ScheduleRequest 
WHERE (ScheduleDateCurrent BETWEEN DATE_ADD(DATE(CURDATE()), INTERVAL 1 DAY)
          AND  DATE_ADD(DATE(CURDATE()), INTERVAL 2 DAY)) 
    AND JobStatus = '3' 
GROUP BY SSR

答案 1 :(得分:1)

“简单”的方式是:

WHERE date(ScheduleDateCurrent) = date_add(CURDATE(), interval 1 day) AND JobStatus = '3' 

更好的方法是:

WHERE JobStatus = '3' AND
      ScheduleDateCurrent >= date_add(CURDATE(), interval 1 day) AND
      ScheduleDateCurrent < date_add(CURDATE(), interval 2 day)

这更好的原因是因为它可以利用JobStatus, ScheduleDateCurrent上的索引(如果有的话)。

答案 2 :(得分:1)

要仅计算明天日期(和非NULL ID)的行,您可以这样做:

SELECT COUNT(ID) as total_count 
FROM HTG_ScheduleRequest 
WHERE (ScheduleDateCurrent = ADDDATE(CURDATE(),1)) 
    AND JobStatus = '3' 
GROUP BY SSR