109

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.

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

Wesley Brian Lachenal
  • 4,381
  • 9
  • 48
  • 81
tzmatt7447
  • 2,329
  • 9
  • 24
  • 31

3 Answers3

247
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.

reko_t
  • 55,302
  • 10
  • 87
  • 77
23
SELECT * FROM table WHERE DATEDIFF(NOW(),colname) > 7;
Sajjad Shirazi
  • 2,657
  • 26
  • 24
14
SELECT SUBDATE('2008-01-02', 7);

OR

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

Result:

2007-12-26

Iswanto San
  • 18,263
  • 13
  • 58
  • 79
Michael Pakhantsov
  • 24,855
  • 6
  • 60
  • 59