There are a lot of questions on how to delete all rows older than 30 days but i can't find anything same with mine so i can fix it
i need to delete some records of messages that are older than 30 days, the column name with the date is named sentOn and the rows in that column looks like this 2018-01-12 12:25:00
How should i format my query to delete all records from the table containing those that are older than 30 days?
DELETE FROM messages WHERE sentOn < '2018-02-21 00:00:00';
would this work?
EDIT:
above query works but very very slowly any way to make it faster? i tried now() but it gives error that the function is wrong
The following code will delete the records of messages that are older than 30 days
DELETE FROM messages WHERE sentOn < NOW() - INTERVAL 30 DAY;
The NOW() method in MySQL is used to pick the current date with time. INTERVAL 30 DAY used for subtracting 30 days from the current date.
After the above query, you can check the current table using the SELECT statement. Thank you!
DELETE FROM messages WHERE sentOn > '2018-02-21 00:00:00';
You want to delete messages that are greater than '2018-02-21 00:00:00'. You can check that the logic is correct first by Select * FROM messages WHERE sentOn > '2018-02-21 00:00:00'.
Related
I have a data table whose structure looks like this.
date time order_id action quantity
How can I query to know how many entries are made to this table previous minute?
Say suppose the time now is 14:46 I want to know how many row entries were made to this table at 14:45. how can I do that?
The problem I am facing now is that I don't know how can I get last minute's time stamp. CURRENT_TIMESTAMP() is giving me correct current time. But I tried CURRENT_TIMESTAMP()-1 which gives some decimal number.
you can use DATE_SUB(), specifying a date and an INTERVAL as documented for DATE_ADD(). Example:
SELECT DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 MINUTE);
You can use my query to find the last minute :
SELECT DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 MINUTE);
I have a system where you can get something for "free" but only once every 7 days, I'm currently having an issue in that once every 7 days part.
What I want to do is delete entries in a certain table once that one or more entry/entries went over 7 days. The concerned table has an ID, USERNAME and DATE column.
Any thoughts?
It should be as easy as:
delete from theTable where date < now() - interval 7 days;
Make sure to run it often enough so that you don't have to delete to many rows.
If you're in an environment without replication you can go ahead and add a limit
delete from theTable where date < now() - interval 7 days limit 1000;
And if this is a large table put an index on date (or where date is first) so it doesn't do a table scan.
Hi i am totally confused with a date logic in my mysql query for a cron job to be run everyday at 12:00 AM
I am working on a auto listing website where the car listings are having a expiry date in mysql datetime format.
All the expired listings will be deleted from the website after 7 days from the datetime of the expiry
When the cron job will run it has do following things
Task 1 - Send an email alert to the users telling them that their listing has expired.
So I need to select all those listings which have expired since last time the cron job has been run and not include listings before that in order to send the expiry alert email only once per listing.
I tried following sql query for this task (Again confused with this as well)
SELECT car_id FROM cars WHERE expiry_date > DATE_SUB(NOW(), INTERVAL 1 DAY) AND expiry_date < NOW()
Task 2 - Will send an email alert to users telling them that listing is going to be permanently deleted after 24 hours.
So I need to select all those listings which are going to be deleted in more than 24 hours / 6 days have passed since they were expired and i need to make sure that they get minimum 24 hours time to renew them. Also i need to select / build the sql query in such a way that only those listings get selected which are going to expiry in 1 days and not other in order to avoid multiple email alerts instead of one time email alert
I tried following sql query for this task (I am totally confused with this query)
SELECT car_id FROM cars WHERE expiry_date > DATE_SUB(NOW(), INTERVAL 7 DAY) AND expiry_date < DATE_SUB(NOW(), INTERVAL 1 DAY)
Task 3 - Delete all the listings which were expired more than 7 days ago
I tried following sql query for this task
SELECT car_id FROM cars WHERE expiry_date < DATE_SUB(NOW(), INTERVAL 7 DAY)
Please help me in perfecting all the 3 queries so that the cron does it job exactly as i want. Also please let me where it has to >= (greater than or equal to) or <= (less than or equal to)
Here is the sqlfiddle table structure and couple of records (though they are not expired yet)
http://sqlfiddle.com/#!2/cfcdf
I will really appreciate the help.
Is this what you are looking for? Please try to add another column to see the differnce between expiry_date and current date time for you to get a better idea of the dates you are dealing with. Please look into some dates functions in MYSQL.
SQLFIDDLE DEMO
-- 3rd query expiry dates older than 7 days from
-- today
SELECT car_id, expiry_Date,
DATE_sub(NOW(), INTERVAL 7 DAY)
FROM cars
WHERE expiry_date <=
DATE_sub(NOW(), INTERVAL 7 DAY)
;
-- same
SELECT car_id, expiry_Date,
DATE_ADD(NOW(), INTERVAL -7 DAY)
FROM cars
WHERE expiry_date <=
DATE_ADD(NOW(), INTERVAL -7 DAY)
;
-- 2nd query going to expire in exactly 1 day
SELECT car_id, expiry_date,
Now() + interval 1 day
FROM cars
WHERE expiry_Date = Now() + interval 1 day
;
-- 1st query: expired
SELECT car_id FROM cars
WHERE expiry_date < Now()
;
-- 1st query: expired last 24 hours
SELECT car_id,DATEDIFF(expiry_date, Now())
FROM cars
WHERE expiry_Date < Now()
AND expiry_Date >= Now() - interval 1 day
;
Check out these queries
select * from cars where datediff(EXPIRY_DATE,now())=-1;
select * from cars where
datediff(DATE_ADD(EXPIRY_DATE, interval 24 hour),now())>=1 and
datediff(DATE_ADD(EXPIRY_DATE, interval 24 hour),now()) <=2;
select * from cars where datediff(expiry_date,now())<=-7;
ope they are working according to your need.
fiddle http://sqlfiddle.com/#!2/785ea/5
There is nothing significantly wrong with your queries, if you do not understand the functions that you have used then google them and read about them until you do.
There is a fundamental problem in your approach in that it relies on the cron job being run at exactly 24 hour intevals - to the milisecond - or there will be double ups and/or omissions.
You need another table to store details of when your batch program last ran; intitialise this with 1 row with a date a long time in the past so that we have a starting point.
You can get the most recent batch by SELECT MAX(date_ran) FROM BatchRecordTables. Store this in a local variable T0. Get the current time, store this in a local variable T1 (Do not use NOW() in multiple queries as they will be slightly differant times and you need them to be the same). I do not know the syntax for this is MySQL - you will have to look it up.
Your situations then become.
Send email to people whose listings have expired since that last time the cron job was run i.e. SELECT car_id FROM cars WHERE Expiry_Date BETWEEN T0 AND T1. This will only select people whoose listings have expired between this batch and the previous one.
For the second case, we need to know that these people have got the first email i.e. that their listing had expired before the last batch run so SELECT car_id FROM cars WHERE Expiry_Date BETWEEN DATE_SUB(T1, INTERVAL 6 DAY) AND T0. This will only select people whoose listings expired before the last batch (i.e. they got the exprired email) and more than 6 days ago.
Same logi applies - we want to know they got the second email. SELECT car_id FROM cars WHERE Expiry_Date BETWEEN DATE_SUB(T1, INTERVAL 7 DAY) AND DATE_SUB(T0, INTERVAL 6 DAY)
May I also suggest that you do not permenantly delete the listings but either copy them to a DeletedListings table or Flag them with a Deleted column - each has its own pros and cons. In the information age, never throw data away - you never know when it might be valuable.
I am looking for a query that is able to delete all rows from a table in a database where timestamp is older than the current date/time or current timestamp.
Would really appreciate some help out here urgently!
Here's the query I am using but as I thought it ain't working:
delete from events where timestamp<CURRENT_TIMESTAMP{);
Um... This may seem silly, but every record in the table will be older than Now(), since Now() is calculated at the time that query is processed. If you you want to delete a record that's older than another record, then you don't want to use Now(), but the timestamp from the record you're comparing the rest to. Or, if you want to delete records that are older than a specific point in time, then you need to calculate the timestamp that you want to use to compare against. For example, to delete records older than 10 minutes, you could use this:
DELETE FROM events WHERE timestamp < (NOW() - INTERVAL 10 MINUTE)
Or, for deleting records that are over a day old:
DELETE FROM events WHERE timestamp < (NOW() - INTERVAL 1 DAY)
For specific points in time (e.g. Oct. 12th, 2012 at 4:15:00 PM GMT), there's a method to do that, but the syntax escapes me, right now. Where's my MySQL manual? :)
delete from events where timestamp < NOW()
should be enough.
DELETE FROM events WHERE timestamp < UNIX_TIMESTAMP(NOW())
or if it's a standard datetime
DELETE FROM events WHERE timestamp < NOW()
Hibernate (hql) Delete records older than 7 days
I am not sure, but you can Try this:
String hqlQuery = "from PasswordHistory pwh "
+ "where pwh.created_date < datediff(curdate(), INTERVAL 7 DAY)";
List<Long> userList = (List<Long>)find(hqlQuery);
deleteAll(userList );// from baseDao
public void deleteAll(Collection list) {
getHibernateTemplate().deleteAll(list);
}
DELETE FROM table WHERE date < '2011-09-21 08:21:22';
I inserted a number of rows 3 hours ago and I don't want these rows to change. How can I write a sql statement that will compare current time with the timestamp in the row and restrict users from changing it if above criteria is met.
Thanks
If you want to do it by using mysql, you will have to use the INTERVAL statement which will allow you to "add" time to date functions... for instance:
UPDATE table
SET data = 'whatever'
WHERE NOW() - INTERVAL 3 HOUR < last_change
You can find more info and examples here: Date and Time Functions
You can use a WHERE clause in all updates:
UPDATE yourtable
SET foo = bar
WHERE inserttime > NOW() - interval 3 hour