Solved

Purge old entries from MySQL table

Posted on 2008-06-12
4
707 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
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…

617 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