MYSQL 查询/早于 1 周前的日期(UTC 中的所有日期时间)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3600481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:00:18  来源:igfitidea点击:

MYSQL query / dates older than 1 week ago (all datetimes in UTC)

mysqldatetimeutc

提问by tzmatt7447

How do I query a mysql db to return all records with a datetime older than 1 week ago. Note that the datetime table stores everything in UTC, and I should be comparing it in that itself.

如何查询 mysql 数据库以返回日期时间早于 1 周前的所有记录。请注意,日期时间表以 UTC 格式存储所有内容,我应该在其本身中进行比较。

Just to be clear - I'm looking for a pure mysql query.

只是要清楚 - 我正在寻找一个纯 mysql 查询。

回答by reko_t

SELECT * FROM tbl WHERE datetime < NOW() - INTERVAL 1 WEEK

If your table stores datetimes in different timezone than what NOW()returns, you can use UTC_TIMESTAMP()instead to get the timestamp in UTC.

如果您的表将日期时间存储在与NOW()返回的时区不同的时区中,您可以UTC_TIMESTAMP()改用以获取 UTC 中的时间戳。

回答by Sajjad Shirazy

SELECT * FROM table WHERE DATEDIFF(NOW(),colname) > 7;

回答by Michael Pakhantsov

SELECT SUBDATE('2008-01-02', 7);

OR

或者

SELECT SUBDATE(now(), INTERVAL 1 week);

Result:

结果:

2007-12-26

2007-12-26