简体   繁体   中英

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!

Have you tried something like this?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM