Truncate with condition

前端 未结 4 1636
野的像风
野的像风 2020-12-08 09:49

truncate ->this resets the entire table, is there a way via truncate to reset particular records/check conditions.

For ex: i want to reset all the data and keep las

4条回答
  •  醉酒成梦
    2020-12-08 10:25

    As a response to your question: "i want to reset all the data and keep last 30 days inside the table."

    you can create an event. Check https://dev.mysql.com/doc/refman/5.7/en/event-scheduler.html

    For example:

    CREATE EVENT DeleteExpiredLog
    ON SCHEDULE EVERY 1 DAY
    DO
    DELETE FROM log WHERE date < DATE_SUB(NOW(), INTERVAL 30 DAY);
    

    Will run a daily cleanup in your table, keeping the last 30 days data available

提交回复
热议问题