Is it true that the following line will delete every event in the table that happend yesterday ?
delete from events where date(event_date) < CURDATE() - INTERVAL 1 DAY;
if not, how can i delete the events that happend yesterday ?
No, your query deletes all events older than yesterday. To delete only the events yesterday do
delete from events
where date(event_date) = CURDATE() - INTERVAL 1 DAY;
For optimization purposes, it is better to use comparisons rather than functions on the column. If event_date
has no time component:
delete from events
where event_date = date_sub(CURDATE(), 'INTERVAL 1 DAY');
If it does have a time compenent:
delete from events
where event_date >= date_sub(CURDATE(), 'INTERVAL 1 DAY') and
event_date < CURDATE();
In particular, this allows MySQL to use an index for the WHERE
clause, which can make the query much more efficient.