select all records created within the hour

☆樱花仙子☆ 提交于 2021-02-08 13:14:35

问题


startTimestamp < date_sub(curdate(), interval 1 hour)

Will the (sub)query above return all records created within the hour? If not will someone please show me a correct one? The complete query may look as follows:

select * from table where startTimestamp < date_sub(curdate(), interval 1 hour);

回答1:


Rather than CURDATE(), use NOW() and use >= rather than < since you want timestamps to be greater than the timestamp from one hour ago. CURDATE() returns only the date portion, where NOW() returns both date and time.

startTimestamp >= date_sub(NOW(), interval 1 hour)

For example, in my timezone it is 12:28

SELECT NOW(), date_sub(NOW(), interval 1 hour);
2011-09-13 12:28:53  2011-09-13 11:28:53

All together, what you need is:

select * from table where startTimestamp >= date_sub(NOW(), interval 1 hour);


来源:https://stackoverflow.com/questions/7405944/select-all-records-created-within-the-hour

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!