Removing rows from MySQL table where the timestamp is over one day old?
I found the exact same question here.
But it isn't working for me. I've modified it a bit, manipulated it, and I can't figure it out. I'm trying to remove rows that are over a day old. Here is my code:
if (isset($_POST['prune'])) {
$sql = "DELETE FROM logs WHERE time < date('now', '-1 days')";
mysql_query($sql);
echo 'Logs older than one day removed.';
}
Fairly simple question I suppose, but its bugging the hell ou开发者_如何转开发t of me. I would appreciate any help.
In case it makes a difference, the column is a TIMESTAMP
type.
EDIT: Apparently I'm an idiot. The question I linked you to relates to SQLite3. So now my question is, how can I do this in MySQL?
You can subtract an interval:
DELETE FROM logs WHERE time < now() - interval 1 day
That answer was IIRC for SQLite3. You're using MySQL which does not support this syntax.
You want to use DATE_ADD() function (example below not tested but should work):
DELETE FROM logs WHERE time < TIMESTAMPADD(DAY,-1,NOW());
In my case, only the
timestampadd
in positive direction works when running through an asc ordered date calendar:
if (urldecode ($aUrl['Select']) == '>')
$this-> db-> where ('konzertdatum >', 'TIMESTAMPADD(DAY, 1, "'. $id . '") ', false) ;
else
$this-> db-> where ('konzertdatum < ', 'TIMESTAMPADD(DAY, -1, "'. $id . '") ', false) ;
In negative direction (-1)
, the timestamp goes back to the very first entry. I also tried not to use CodeIgniter, but the same consequence.
Summing up, +1
as parameter for timestampadd works, -1
works for the second lowest entry (then jumping back to the first one. But it fails when I step further away from the first date.
精彩评论