I want to delete all records in a database if the timestamp is older than 4 hours.
So my logic is to get the hour of the current time and get the hour of from the timestamp saved in the database and subtract to see if it is greater than 4. If it is greater than 4 than delete the records.
This is a code work in progress not really sure if it is correct.
DELETE FROM posts
WHERE id IN
(SELECT *
FROM posts
WHERE (HOUR(CURRENT_TIMESTAMP) - HOUR(time_published)) > 4)
)
if it makes a difference I am using MySQL.
Why not a simple
delete
from posts
where timestampdiff(hour, current_timestamp, time_published)>=4
Note that comparing the hour portions of date fields won't do what you expect. Consider comparing 21st Jan 1985 10:00 and 22nd Jan 1985 11:00. Your original condition would fail (1 hour), but it's actually 25 hours between them.
If you save the record at timestamp 1:00' and run this query at 5:59' nothing will be removed because the time difference is 4:59' and the hour component of that is 4! It might be what you want but that's way closer to 5 hours than 4.
Assuming that minutes are your smallest unit of precision, you might want to do something like
DELETE FROM posts
WHERE TIMESTAMPDIFF(MINUTE, CURRENT_TIMESTAMP, time_published) > 240
And use nested queries only when they are absolutely necessary; they possibly could slow your operations down drastically.
Related
I have an event that runs every hour on my database. It calls a procedure that deletes rows from my photo table older than a day. Here is the query:
DELETE FROM Photos WHERE created_at < (NOW() - INTERVAL 1 DAY);
However, this morning I noticed that the row I was testing was deleted more than 5 hours before it should have been. I'm living in Houston so i'm on central time. The photo should have deleted at 11:23 AM central so i'm guessing this is a timezone issue. If it is how can add a timezone on to the date, if it isn't what could be the problem?
It could be a time zone issue if you dB is not on the same timezo e you are on.
Is it imperative that the data is being cleaned every hour rather than every day.
You might get more joy out of using the date part functionality getting just the day out. This way you get rid of the time stamp completely
So
Delete from photos where created_at < ((date(now()) - INTERVAL 1 DAY );
I have a table in Mysql which has column called 'dep_timestamp' which holds data in the following format (the data is received from a external source so can't be changed, and is displayed via web queries so can't be modified within that table)
2015-05-12 19:18:00 +0100
The database holds cancellations for booked taxi journeys which get pushed out to me from a central booking system in realtime. Throughout the day I will get any number of messages for cancelled journeys. A journey has a booked departure time dep_timestamp in its full format of 2015-05-12 19:18:00 +0100 that is used for reporting and all sort of other things.
Every day at 03:00 I want to delete all of the cancelled journeys that where due to depart 'yesterday' This means when my users do a query and ask what journeys have been cancelled today they only see stuff that has a booked departure of today.
I have an event setup on the server to delete rows older then 1 day using the following code;
DELETE FROM db.canx_today WHERE 'dep_timestamp' < DATE_SUB(CURRENT_TIME() , INTERVAL 1 DAY)
That event is set to run every day at 03:00 and does without error. However it takes the full date/time into consideration when running which means it only deletes the rows where the time & date are both older than one day.
If I swap CURRENT_TIME with CURRENT_DATE then the server throws this error; Truncated incorrect datetime value: '2015-05-13 10:17:00 +0100' which makes sense in so far that its looking for a full date/time string.
Is there a way to ignore the time element and just delete all rows that are from the previous day?
You can calculate based on CURRENT_DATE() and just concatenate 00:00:00 to that value.
WHERE `dep_timestamp` < CONCAT(CURRENT_DATE(), ' 00:00:00')
This should work, but will only be noticeably faster than the one I originally put in the comments above if dep_timestamp is indexed.
WHERE `dep_timestamp` < DATE_FORMAT(curdate(), "%Y-%m-%d 00:00:00")
Since DATE_FORMAT() actually returns a string, this might be more efficient when indexes are actually needed:
WHERE `dep_timestamp` < CAST(DATE_FORMAT(curdate(), "%Y-%m-%d 00:00:00") AS DATETIME)
DELETE FROM `canx_today`
WHERE DATE(`dep_timestamp`) = DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY);
I have a column with timestamp, contain example value "2014-04-16 18:00:00","2014-04-17 18:00:00"....
Now, if I will call a page before "2014-04-17 12:00:00" I need this value-"2014-04-16 18:00:00"
And if I call my page after "2014-04-17 12:00:00" I need this value "2014-04-17 18:00:00".
I think my question is very complicated to understand, having complications in date & times, please check date & time properly.
I want to fetch this data from DB in mysql, The page I was saying is that where I'm going to add your mysql query.
Thanx in advance
Generalising what your asking for a bit the following will return dates from the previous day if it's before noon and dates from today if it's after noon:
SELECT date_column
FROM yourTable
WHERE DATE(DATE_SUB(NOW(), INTERVAL 12 HOUR)) = DATE(date_column);
Edit:
The WHERE clause First gets the current time (NOW()) and subtracts 12 hours. This wont affect the date unless the time is before 12. This means DATE_SUB(NOW(), INTERVAL 12 HOUR) gives us today if it's after noon and yesterday if it's before.
We then check if the date_column matches the date we've created (using the DATE function so that the time is ignored).
Adding some rows to the SELECT may help you see how these dates are built up.
I have a database table that includes a timestamp for each record. Everyday this database is updated by a cron that was set to run 1 minute before gmt midnight (23:59:00). We are changing the cron to run at exactly midnight now (00:00:00), so I need to update all fields that were logged at 23:59 to 00:00 of the next day (2013-05-21 23:59:00 should update to 2013-05-22 00:00:00).
The update script was set to capture the timestamp at script start, but because it was poorly written it didn't account for seconds so some records have a start time of 2013-05-21 23:59:01, some may have 2013-05-20 23:59:02 or even 2013-05-19 23:59:03. All of these will need to be updated to 00:00:00 of the next day.
There are thousands of other records that were not updated by the cron and therefore have random timestamps. These records need to be left unaffected. For example 2013-05-19 23:13:47, 2013-05-19 02:50:56, and 2013-05-19 16:42:13 should all be left untouched.
I think the following code from this post is somewhat along the lines of what I'm looking for, but after some googling and testing myself I haven't had much luck.
UPDATE table
SET `time` = CASE
WHEN CURRENT_TIMESTAMP>='23:59:00'
THEN CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ELSE CURRENT_TIMESTAMP END
Try this query -
UPDATE
table
SET
`time` = DATE(`time`) + INTERVAL 1 DAY
WHERE
TIME(`time`) >= '23:59:00'
This might be simple but my brain has melted after a long day of non stop coding, and I'm running out of paper fast...
I'm trying to figure out a yearly expiry formula to calculate in a stored procedure.
Simplified table:
Security_Table
-----------------
User_ID [int]
Join_Date [DateTime]
Expired [VARCHAR]
So if a user joined on 2010-01-11 Expired would update to "TRUE" today, same goes for someone who joined on 2009-01-11 as it's a recurring expiry.
I'm running the procedure on a daily basis through scheduled tasks, would comparing the day and month be suffice? Obviously accounting for a leap year.
UPDATE Security_Table SET Expired = 'TRUE' WHERE DATE_FORMAT(Join_Date,'%m-%d') = DATE_FORMAT(NOW(),'%m-%d')
Thanks guys.
Yes, you can use WHERE month = month and date = date. (Though be careful of 29th Feb as a join date.)
This does, however, mean you have to scan to whole table/index looking for matches. If you have a large table, this may be a problem.
I would think that in this case you're better off setting an expiry date value. Then checking that...
WHERE
expiry_date <= CURDATE()
When some-one renews you can update the expiry. SET expiry_date = DATE_ADD(expiry_date, INTERVAL 1 YEAR). You may have a new offer for 13 months for the price of 12, and setting the expiry lets you be flexible like that. It's even immune to the 29th Feb problem. In terms of reliability; if your batch process fails, running it a day late won't make you miss a bunch of people...
This query will simply check the Month and Day of every Join_Date against today, If you joined on Feb 29th, it will expire on Feb 28th or it will take 4 years to expire.
UPDATE Security_Table
SET Expired='TRUE'
WHERE Expired='FALSE'
AND
(
(DATE_FORMAT(Join_Date,'%m-%d')='02-29' AND DATE_FORMAT(NOW(),'%m-%d')='02-28')
OR
(DATE_FORMAT(Join_Date,'%m-%d') = DATE_FORMAT(NOW(),'%m-%d'))
);
You should also index the table so that only those records that have Expired='FALSE' are examined.
ALTER TABLE Security_Table ADD INDEX (Expired,Join_Date);
Give it a Try !!!