Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 out 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?

like image 722
Rob Avatar asked May 08 '10 21:05

Rob


People also ask

How do I purge old data in MySQL?

Let say you want to delete data that over 31 days old. CREATE TABLE delete_keys SELECT id FROM my_table WHERE 1=2; INSERT INTO delete_keys SELECT id FROM ( SELECT id FROM my_table WHERE time_stored < (UNIX_TIMESTAMP() - 2678400) ORDER BY time_stored ) A LIMIT 100; ALTER TABLE delete_keys ADD PRIMARY KEY (id); DELETE B.

How do you delete records older than 30 days in SQL?

To delete all rows older than 30 days, you need to use the DELETE with INTERVAL. Use < now() i.e. less than operator to get all the records before the current date.

How do I delete rows faster in MySQL?

To delete rows in a MySQL table, use the DELETE FROM statement: DELETE FROM products WHERE product_id=1; The WHERE clause is optional, but you'll usually want it, unless you really want to delete every row from the table.

Which clause will you use to delete a single record from a table?

The DELETE Statement in SQL is used to delete existing records from a table.


1 Answers

You can subtract an interval:

DELETE FROM logs WHERE time < now() - interval 1 day
like image 140
Mark Byers Avatar answered Oct 01 '22 13:10

Mark Byers