Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-20-2012, 11:56 AM   PM User | #1
Feckie
Regular Coder

 
Join Date: Jan 2009
Posts: 196
Thanks: 29
Thanked 0 Times in 0 Posts
Feckie has a little shameless behaviour in the past
Delete HOUR or DAY ?

I need to delete all where timestamp is over 35hrs old, as the links are then void..
I have tried
$query = "DELETE FROM import WHERE Timestamp < NOW() - INTERVAL 35 HOUR";
But it seems to delete all over 24hrs old

When I use
$query = "DELETE FROM import WHERE Timestamp < NOW() - INTERVAL 3 DAY";
The older links are dead.

Any ideas

Last edited by Feckie; 07-21-2012 at 02:13 PM..
Feckie is offline   Reply With Quote
Old 07-20-2012, 12:09 PM   PM User | #2
MarkR
New Coder

 
Join Date: Sep 2011
Posts: 80
Thanks: 0
Thanked 13 Times in 12 Posts
MarkR is an unknown quantity at this point
Off the top of my head, date_add any good?

Code:
WHERE DATE_ADD(Timestamp, INTERVAL 35 HOUR) < NOW()
__________________
Web Design Newcastle
MarkR is offline   Reply With Quote
Users who have thanked MarkR for this post:
Feckie (07-21-2012)
Old 07-20-2012, 12:14 PM   PM User | #3
Feckie
Regular Coder

 
Join Date: Jan 2009
Posts: 196
Thanks: 29
Thanked 0 Times in 0 Posts
Feckie has a little shameless behaviour in the past
Quote:
Originally Posted by MarkR View Post
Off the top of my head, date_add any good?

Code:
WHERE DATE_ADD(Timestamp, INTERVAL 35 HOUR) < NOW()
That Adds or subtracts a specified time interval from a date, what i want to do,
is Delete from database files older than 35 hours
Feckie is offline   Reply With Quote
Old 07-20-2012, 02:58 PM   PM User | #4
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
Have you tried doing a SELECT * FROM import where Timestamp < NOW() - INTERVAL 35 HOUR? It looks like it might be a timezone issue. Also try just a SELECT NOW() - INTERVAL 35 HOUR, and make sure it looks good.
Keleth is offline   Reply With Quote
Users who have thanked Keleth for this post:
Feckie (07-21-2012)
Old 07-20-2012, 07:31 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,188
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Mark's answer is correct.

He was only showing the WHERE part of the query.

The full query would be
Code:
DELETE FROM import WHERE DATE_ADD(Timestamp, INTERVAL 35 HOUR) < NOW()
But Keleth's answer should have worked, too.

Part of your problem may be that timestamp is a reserved word in MySQL. You really should try to avoid using reserved words as field and table names.

If that is the problem, though, you can just enclosed the name in BACKTICKS -- the ` character that shares a key with the ~ tilde.
Code:
DELETE FROM import WHERE DATE_ADD(`Timestamp`, INTERVAL 35 HOUR) < NOW()
or
Code:
DELETE FROM import WHERE `timestamp` < NOW() - INTERVAL 35 HOUR
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
Feckie (07-21-2012)
Old 07-21-2012, 08:16 AM   PM User | #6
Feckie
Regular Coder

 
Join Date: Jan 2009
Posts: 196
Thanks: 29
Thanked 0 Times in 0 Posts
Feckie has a little shameless behaviour in the past
Hi Old Pedant,

I have just to admit what a plonker I am..

mindset 1 day = 12 hrs Must be getting older than I thought

now set to 3 days = 72hrs so set to delete to 71hrs and it's working

Go have a good laugh... :-)

Last edited by Feckie; 07-21-2012 at 02:12 PM..
Feckie is offline   Reply With Quote
Old 07-21-2012, 02:09 PM   PM User | #7
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
Can you show us some sample rows from your table and which ones are being deleted that shouldn't be? Something doesn't seem right and it isn't the code above.

the DATE_ADD as written takes all dates and adds 35 hours to them. If the dates are within 35 hours of now(), adding the 35 hours will bring them to a value greater than now() and thus would fail the WHERE clause and not be deleted.
guelphdad is offline   Reply With Quote
Users who have thanked guelphdad for this post:
Feckie (07-21-2012)
Old 07-21-2012, 02:39 PM   PM User | #8
Feckie
Regular Coder

 
Join Date: Jan 2009
Posts: 196
Thanks: 29
Thanked 0 Times in 0 Posts
Feckie has a little shameless behaviour in the past
Smile

Quote:
Originally Posted by guelphdad View Post
Can you show us some sample rows from your table and which ones are being deleted that shouldn't be? Something doesn't seem right and it isn't the code above.

the DATE_ADD as written takes all dates and adds 35 hours to them. If the dates are within 35 hours of now(), adding the 35 hours will bring them to a value greater than now() and thus would fail the WHERE clause and not be deleted.
ha ha read above
Feckie is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:43 AM.


Advertisement
Log in to turn off these ads.