MySQL Delete Records Older Than X Minutes?

前端 未结 5 473
攒了一身酷
攒了一身酷 2020-12-30 08:18

I\'ve searched quite a bit and found a few solutions that did not end up working for me and can\'t understand why.

I have a table with a timestamp column. The MySQL

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-30 08:28

    timestamp is a reserved keyword in mysql. To use timestamp as a field name, you have to put that in backticks as shown below.

    `timestamp`
    

    If time_created is a unix timestamp (int), you should be able to use something like this:

    DELETE FROM adminLoginLog WHERE `timestamp` < (UNIX_TIMESTAMP() - 600);
    

    (600 seconds = 10 minutes - obviously)

    Otherwise (if time_created is mysql timestamp), you could try this:

    DELETE FROM adminLoginLog WHERE `timestamp` < (NOW() - INTERVAL 10 MINUTE)
    

    Update 1

    DELETE FROM adminLoginLog WHERE `timestamp` < DATE_SUB( CURRENT_TIME(), INTERVAL 10 MINUTE)
    

    Update 2

    DELETE FROM adminLoginLog WHERE `timestamp` < DATE_SUB( NOW(), INTERVAL 10 MINUTE)
    

    Demo

提交回复
热议问题