Solved

Purge old entries from MySQL table

Posted on 2008-06-12
4
665 Views
Last Modified: 2012-06-21
I need to delete entries from a table which are over 24 hours old

i just cant get my head around all the different mysql date/time functions

the date field is type; timestamp
with CURRENT_TIMESTAMP as default
0
Comment
Question by:rlambell
  • 2
4 Comments
 
LVL 19

Expert Comment

by:elimesika
ID: 21774445
Assuming your table is T amd timestamp field is t

delete from T where t < "%s"' % int(time.time() - 24)
0
 

Author Comment

by:rlambell
ID: 21774510
i cant get that to work, simple syntax error? tried variations on quotes around %s but didnt work

running this:
delete from search_logs where search_date < "%s"' % int(time.time() - 24)

returned this:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' % int(time.time() - 24)' at line 1

im using MySQL 5.0.45-community
0
 
LVL 19

Accepted Solution

by:
elimesika earned 250 total points
ID: 21776295
HI

Sorry, my mistake

here it is


delete from search_logs where TIMESTAMPADD(HOUR,24,search_date) < NOW()

Open in new window

0
 
LVL 18

Assisted Solution

by:philipjonathan
philipjonathan earned 250 total points
ID: 21776377
Alternatively:
delete from search_logs where search_date + INTERVAL 24 HOUR < NOW()

The benefit of using this statement is you can add an index on the column search_date to speed up your query.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article describes some very basic things about SQL Server filegroups.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now