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

后端 未结 3 854
悲哀的现实
悲哀的现实 2020-12-23 02:37

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

相关标签:
3条回答
  • 2020-12-23 03:11
    SELECT SUBDATE('2008-01-02', 7);
    

    OR

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

    Result:

    2007-12-26

    0 讨论(0)
  • 2020-12-23 03:21
    SELECT * FROM table WHERE DATEDIFF(NOW(),colname) > 7;
    
    0 讨论(0)
  • 2020-12-23 03:33
    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.

    0 讨论(0)
提交回复
热议问题