Solved

Purge old entries from MySQL table

Posted on 2008-06-12
4
693 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
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…

756 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