我在sql方面不是很好,所以我想知道这些陈述是否适用于选择curent以及之前的小时,日期和月份?
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那么我需要计算:
DATE_SUB(NOW(), INTERVAL '0' HOUR) <= ADDTIME(Datestamp, Timestamp)
DATE_SUB(NOW(), INTERVAL '1' HOUR) <= ADDTIME(Datestamp, Timestamp)
从日期和飞蛾开始,例如,如果当前是2013-01-08,那么我需要计算2013-01-08和2013-01-07
答案 0 :(得分:6)
以下是如何使用MySQL date functions创建日期的示例:
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')
同样适用于其他情况。
答案 1 :(得分: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))
答案 2 :(得分: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')