Latchy Latchy - 3 months ago 16
MySQL Question

MySQL select records from last month or before

I want to run a CRON job at the beginning of every month to select all the records that are from the last month or even prior to that. The script runs at ~00:15am on the first of the month but the SELECT must not include records that may have been created within that ~15 mins. The column I'm running the condition against is stored as datetime and the database is MySQL.

EDIT:

Example:

rowID | time
---------------------------
6 | 2016-06-01 00:12:21
5 | 2016-06-01 00:04:34
4 | 2016-05-28 19:46:45
3 | 2016-05-17 19:25:01
2 | 2016-05-08 06:33:32
1 | 2016-04-25 12:22:54


Basically, looking for all rows where ID < 5.

SELECT rowID
FROM table
WHERE time < beginning_of_current_month;


Thanks in advance!

Answer

Have you tried something like this?

select rowID from table
where time < DATE_FORMAT(NOW(), '%Y-%m-01')