2

我的sql不太好,所以我想知道这些语句对于选择当前和前一个小时、天和月是否正确?

DATE_SUB(NOW(), INTERVAL '1' HOUR) <= ADDTIME(Datestamp, Timestamp)
DATE_SUB(NOW(), INTERVAL '1' DAY) <= ADDTIME(Datestamp, Timestamp)
DATE_SUB(NOW(), INTERVAL '1' MONTH) <= ADDTIME(Datestamp, Timestamp)

例如,如果现在是 14:49,那么我需要计算:

  1. 所有从 14:00 到 15:00(或直到 14:49)-DATE_SUB(NOW(), INTERVAL '0' HOUR) <= ADDTIME(Datestamp, Timestamp)
  2. 全部从 13:00 到 14:00DATE_SUB(NOW(), INTERVAL '1' HOUR) <= ADDTIME(Datestamp, Timestamp)

与天和蛾相同,例如如果当前是 2013-01-08,那么我需要计算 2013-01-08 和 2013-01-07

4

3 回答 3

6

这是一个如何使用MySQL 日期函数来创建日期的示例:

SELECT 
    NOW() AS CurrentDateTime, 
    DATE_FORMAT(NOW(), '%Y-%m-%d %H:00:00') AS CurrentHour, 
    DATE_FORMAT(NOW(), '%Y-%m-%d %H:00:00') - INTERVAL 1 HOUR AS PreviousHour, 
    CURDATE() AS CurrentDate, 
    CURDATE() - INTERVAL 1 DAY AS PreviousDate, 
    DATE_FORMAT(NOW(), '%Y-%m-01') AS CurrentMonth, 
    DATE_FORMAT(NOW(), '%Y-%m-01') - INTERVAL 1 MONTH AS PreviousMonth

输出:

CurrentDateTime: 2013-01-08 21:00:51
    CurrentHour: 2013-01-08 21:00:00
   PreviousHour: 2013-01-08 20:00:00
    CurrentDate: 2013-01-08
   PreviousDate: 2013-01-07
   CurrentMonth: 2013-01-01
  PreviousMonth: 2012-12-01

这是示例用法:

-- SELECT PREVIOUS HOUR ROWS
SELECT *
FROM table1
WHERE ADDTIME(Datestamp, Timestamp) >= DATE_FORMAT(NOW(), '%Y-%m-%d %H:00:00') - INTERVAL 1 HOUR
AND   ADDTIME(Datestamp, Timestamp) <  DATE_FORMAT(NOW(), '%Y-%m-%d %H:00:00')

-- SELECT CURRENT HOUR ROWS
SELECT *
FROM table1
WHERE ADDTIME(Datestamp, Timestamp) >= DATE_FORMAT(NOW(), '%Y-%m-%d %H:00:00')

对于其他情况也是如此。

于 2013-01-08T16:01:39.897 回答
3

当前加上前一小时:

ADDTIME(Datestamp, Timestamp) BETWEEN ADDDATE(DATE(NOW()), INTERVAL HOUR(NOW())-1 HOURS) AND NOW()

今天加上昨天:

ADDTIME(Datestamp, Timestamp) BETWEEN DATE(DATE_ADD(DATE(NOW()), INTERVAL -1 DAY)) AND NOW()


编辑:添加了单个查询

当前时间(包括未来):

ADDTIME(Datestamp, Timestamp) >= ADDDATE(DATE(NOW()), INTERVAL HOUR(NOW()) HOURS)

上一个小时:

DateStamp = DATE(NOW()) AND HOUR(Timestamp) = HOUR(NOW())

当前日期:

Datestamp = DATE(NOW())

最后一天:

Datestamp = DATE(DATE_ADD(NOW(), INTERVAL -1 DAY))
于 2013-01-08T13:21:18.457 回答
0

获取当前时间开始的正确方法是:

SUBDATE(SUBDATE(SUBDATE(NOW(),
        INTERVAL EXTRACT(MICROSECOND FROM NOW()) MICROSECOND),
        INTERVAL EXTRACT(SECOND FROM NOW()) SECOND),
        INTERVAL EXTRACT(MINUTE FROM NOW()) MINUTE))

所以要获得一个完整的小时范围,你会做类似的事情

SUBDATE(SUBDATE(SUBDATE(SUBDATE(NOW(),
        INTERVAL EXTRACT(MICROSECOND FROM NOW()) MICROSECOND),
        INTERVAL EXTRACT(SECOND FROM NOW()) SECOND),
        INTERVAL EXTRACT(MINUTE FROM NOW()) MINUTE),
        INTERVAL 1 HOUR) <= my_date
    AND my_date < SUBDATE(SUBDATE(SUBDATE(NOW(),
        INTERVAL EXTRACT(MICROSECOND FROM NOW()) MICROSECOND),
        INTERVAL EXTRACT(SECOND FROM NOW()) SECOND),
        INTERVAL EXTRACT(MINUTE FROM NOW()) MINUTE)

这实际上非常痛苦,如果您不太了解数据类型的纯度,您将使用格式化技巧来获取当前时间:

STR_TO_DATE(DATE_FORMAT(NOW(), '%Y%m%d%H'), '%Y%m%d%H') <= my_date
AND my_date <= ADDDATE(STR_TO_DATE(DATE_FORMAT(NOW(), '%Y%m%d%H'), '%Y%m%d%H'), INTERVAL 1 HOUR)

和前一小时:

SUBDATE(STR_TO_DATE(DATE_FORMAT(NOW(), '%Y%m%d%H'), '%Y%m%d%H'), INTERVAL 1 HOUR) <= my_date
AND my_date < STR_TO_DATE(DATE_FORMAT(NOW(), '%Y%m%d%H'), '%Y%m%d%H')

当天:

STR_TO_DATE(DATE_FORMAT(NOW(), '%Y%m%d'), '%Y%m%d') <= my_date
AND my_date <= ADDDATE(STR_TO_DATE(DATE_FORMAT(NOW(), '%Y%m%d'), '%Y%m%d'), INTERVAL 1 DAY)

和前一天:

SUBDATE(STR_TO_DATE(DATE_FORMAT(NOW(), '%Y%m%d'), '%Y%m%d'), INTERVAL 1 DAY) <= my_date
AND my_date < STR_TO_DATE(DATE_FORMAT(NOW(), '%Y%m%d'), '%Y%m%d')

当月:

STR_TO_DATE(DATE_FORMAT(NOW(), '%Y%m'), '%Y%m') <= my_date
AND my_date <= ADDDATE(STR_TO_DATE(DATE_FORMAT(NOW(), '%Y%m'), '%Y%m'), INTERVAL 1 MONTH)

和上个月:

SUBDATE(STR_TO_DATE(DATE_FORMAT(NOW(), '%Y%m'), '%Y%m'), INTERVAL 1 MONTH) <= my_date
AND my_date < STR_TO_DATE(DATE_FORMAT(NOW(), '%Y%m'), '%Y%m')
于 2013-01-08T13:57:02.410 回答