Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Deleting Old MySql Records With PERL

by chacham (Prior)
on Jul 14, 2016 at 14:39 UTC ( [id://1167783]=note: print w/replies, xml ) Need Help??


in reply to Deleting Old MySql Records With PERL

DELETE FROM table WHERE DATE (NOW (), date) >30

That's a terrible approach; it evaluates the expression for each record.

DELETE FROM table WHERE DATE < DATE_SUB(NOW(), INTERVAL 30 DAY)

That is the correct approach. Evaluate the expression just once, and do a simple comparison.

Run the statement from the console to determine if it is working. If it is not, break it apart and figure out why. Obviously, "DATE_SUB(NOW(), INTERVAL 30 DAY)" should be checked to work. Look at the documentation if you do not understand DATE_SUB()

After you are clear that it should work, try it within your script, where the issues would mainly be connectivity or rights related, having already solved any syntactical or type issues.

  • Comment on Re: Deleting Old MySql Records With PERL

Replies are listed 'Best First'.
Re^2: Deleting Old MySql Records With PERL
by Milti (Sexton) on Jul 15, 2016 at 16:55 UTC

    Thanks for all the feedback. I believe I now have it figured out.

    I added a column to the table and named it DateAdded with Type - TIMESTAMP, NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP. The comparison you suggested now seems to work in that it does not delete rows that were placed in the table today. After a couple of days I will change the INTERVAL to 2 or 3 days and see what happens when I try the query in the Mysql client. I still have a column named 'Posted' which contains a date in simple English, i.e. Month Day, Year.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1167783]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-24 05:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found