I have a field in a sql database called time_stamp that timestamps when the record was added to the database. I have another field called 'TTL', which will have either 1, 2, or 3 stored in it (each number within this field represents a day). Can I have a php script where the deletion of the record is determined by the 'TTL' field?
For instance a record is added at noon today, with a 'TTL' of 2, can I have a script that deletes that same record at noon two days from now?
What would be the best way to do this, and do I really need both fields to execute this?
Your best method for running this script is attaching it to cron and having it run on a given interval.
Then, in order to program the record deletion, you'll need to run the following query:
DELETE FROM records WHERE UNIX_TIMESTAMP(time_stamp) + TTL * 86400 <= CURRENT_TIMESTAMP()
time_stamp is your record creation value - let's say it's equal to 1000000. 86400 is the amount of seconds in a day. TTL is the amount of days after time_stamp that you want the file deleted. So if TTL is 2 and 1000000 was the timestamp for July 10th, we are basically comparing:
1000000 + 86400 * 2 <= CURRENT_TIMESTAMP
Now, let's say that today is July 12th. This means it's 2 days (86400 * 2 seconds) after July 10th. Thus,
CURRENT_TIMESTAMP = July 10th timestamp + 2 days worth of seconds
= 1000000 + 86400 * 2
Which implies CURRENT_TIMESTAMP >= 1000000 + 86400 * 2 for all points in time during and after July 12th, thus deleting the record.
This will delete the records daily for you while your server is running. Enjoy and good luck!
Related
I am new to SQL and I want to update the Amount in the amount field. For example in every month in 3 months I want to add 100 to THE 500 in the amount field in the database. And when the 3rd month comes It will automatically transfer the data to another table
This is the the code I have done so far, but its not working
CREATE EVENT myevent
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP
ENDS CURRENT_TIMESTAMP + INTERVAL 2 MINUTE
DO
UPDATE message
SET amount*3
WHERE date = NOW();
Try to change the update command into:
UPDATE Message
SET amount = amount + 100
WHERE DATEDIFF(CURDATE(), date) >=value
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 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.
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 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'