tags:

views:

125

answers:

1

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( CURRENT_TIME(), INTERVAL 10 MINUTE)

Didn't work. What am I doing wrong?

EDIT: I used this code:

SELECT time_created, CURRENT_TIMESTAMP, TIMESTAMPDIFF( MINUTE, time_created, CURRENT_TIMESTAMP ) FROM locks

But oddly, this gives the wrong result too

time_created          CURRENT_TIMESTAMP      TIMESTAMPDIFF( MINUTE, time_created,     CURRENT_TIMESTAMP )
2010-08-01 11:22:29   2010-08-08 12:00:48   10118
2010-08-01 11:23:03   2010-08-08 12:00:48   10117
+2  A: 

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)
Ivar Bonsaksen
Not sure if that works right, UNIX_TIMESTAMP() outputs number, i guess in milliseconds and the time_create[timestamp] field shows time as 2010-08-01 11:22:29
gAMBOOKa
second code doesn't work either.
gAMBOOKa
Hmm... Try `SELECT time_created, (NOW() - INTERVAL 10 MINUTE), DATE_SUB( CURRENT_TIME(), INTERVAL 10 MINUTE) from locks;` Does any of them provide compareable results? In my experience the middle one will give you a mysql timestamp, while the last one returns `NULL`.
Ivar Bonsaksen