SQL获取特定时间的当前日期

时间:2014-05-07 11:06:08

标签: mysql sql

我正在尝试获得在上午8点到下午4点之间批准的订单

SELECT 
    * 
FROM 
    orders 
WHERE 
    approved_date BETWEEN 'CURDATE() 8:00:00' AND 'CURDATE() 16:00:00'

在这里,我试图获得在下午4点之后(直到下午8点工作日)批准的订单

SELECT 
    * 
FROM 
    orders 
WHERE 
    approved_date BETWEEN 'CURDATE() 16:00:00' AND 'NEXT_WORKING_DAY 8:00:00'

如何正确编写这些查询?

approved_date DATETIME 格式(0000-00-00 00:00:00) 使用MySQL

编辑:工作日=星期一星期二星期四星期四星期四

3 个答案:

答案 0 :(得分:1)

使用timestampadd

timestampadd(HOUR, 16, CURDATE()) # 4 pm
timestampadd(HOUR, 32, CURDATE()) # 8 am the next day

答案 1 :(得分:1)

将其用作:

timestampadd(HOUR, 16, CURDATE()) # same day 16:00

timestampadd(HOUR, 32, CURDATE()) # next day 8:00

答案 2 :(得分:0)

这个怎么样?

 WHERE date(approved_date) = CURDATE() AND
       time(approved_date) BETWEEN '8:00:00' AND '16:00:00'

我不明白" NEXT_WORKING_DAY"" NEXT_WORKING_DAY"应该是的意思。也许就是这样:

WHERE date(approved_date) = CURDATE() AND time(approved_date) >= '8:00:00' OR
      date(approved_date = CURDATE() + 1 AND time(approved_date) <= '16:00:00'