MySQL: Delete all rows older than 10 minutes

后端 未结 2 598
[愿得一人]
[愿得一人] 2021-01-30 02:12

I have a timestamp field in my table. How do I delete records older than 10 minutes old?

Tried this:

DELETE FROM locks WHERE time_created < DATE_SUB(          


        
相关标签:
2条回答
  • 2021-01-30 02:43

    The answer is right in the MYSQL manual itself.

    "DELETE FROM `table_name` WHERE `time_col` < ADDDATE(NOW(), INTERVAL -1 HOUR)"
    
    0 讨论(0)
  • 2021-01-30 02:58

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

    DELETE FROM locks WHERE time_created < (UNIX_TIMESTAMP() - 600);
    

    (600 seconds = 10 minutes - obviously)

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

    DELETE FROM locks WHERE time_created < (NOW() - INTERVAL 10 MINUTE)
    
    0 讨论(0)
提交回复
热议问题