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';
Related
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'.
I want to delete the records that is older than 1 day. What is the best way to achieve it? I have never used event before so i am having little problem.
For eg: I want to delete records where start_time is older than 1 day.
by doing some research i got to this point.
CREATE EVENT deleteRecords
ON SCHEDULE EVERY 1 DAY
ON COMPLETION PRESERVE
DO
DELETE FROM databaseName.tableName WHERE start_time < DATE_SUB(NOW(),
INTERVAL 1 DAY)
There are many ways on how to achieve your desired result.
//sample 1
DELETE FROM databaseName.tableName WHERE DATE_ADD(start_time,INTERVAL 1 DAY) < NOW();
//sample 2
DELETE FROM databaseName.tableName WHERE ADDTIME(start_time,"1 00:00:01") =< NOW();
The code in your post is okay to use too. But don't forget to backup data first if you're unsure of what you're doing.
Reference Date and Time Functions
I want to UPDATE my data in scheduled time. My problem is that I cant equal the date that I enter in my database in the current real time date. For example, I have 2015/24/9 19:50:00 in my database, now I want to equal it to the current real time date so that I can update a specific row in the database. If I don't do that, the amount field will just multiply 5 in every row. I want to multiply the amount by 5 in a specific row and time
Code:
CREATE EVENT myeventsdasa11s
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DO
UPDATE messagesd
SET amount = amount*5
WHERE DATE = (the the current real time date);
DATETIME and TIMESTAMP values are, like floating-point values, difficult to compare for numerical equality. In other words, if it happens that NOW() = datetimestamp, it's a lucky accident. This is especially true when processing events: the event actually starts to run shortly after the scheduled time.
So, instead of saying something like this
`DATE` = NOW()
say something like this
`DATE` BETWEEN NOW() - INTERVAL 10 SECOND
AND NOW() + INTERVAL 10 SECOND
Of course, such a narrow time interval makes you critically dependent on the time accuracy of the event scheduler. You'd be better off adding a LAST_UPDATED column of DATETIME type to your table, then doing this update.
UPDATE messagesd
SET amount = amount * 5,
LAST_UPDATED = `DATE`
WHERE `DATE` >= NOW() - INTERVAL 1 MINUTE
AND (`DATE` > LAST_UPDATED OR LAST_UPDATED IS NULL)
That way, every time your event runs you'll update all the rows that are due for update, but haven't yet been updated. This is not dependent on the precise time an event runs. The - INTERVAL 1 MINUTE allows the event to be up to a minute late running and still function correctly.
If you need to schedule another update for the future for a particular row, change the value of the DATE column but don't touch the LAST_UPDATED column.
Are you just looking for CURDATE()?
CREATE EVENT myeventsdasa11s
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DO
UPDATE messagesd
SET amount = amount * 5
WHERE DATE = CURDATE();
If you need the current real date time use mysql NOW()
CREATE EVENT myeventsdasa11s
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DO
UPDATE messagesd
SET amount = amount*5
WHERE DATE = NOW();
I have a database with several tables. Each of these tables has a column 'created' which contains time-stamps of when that particular row was created in the database.
Now, I want to create a MySQL script that checks once every week if there is data coming into these tables. So, there should be data coming everyday. How do I create a MySQL script that allows me to do this for all the tables in the database?
Note: Remember I want to do this for all the tables in the database with a single script. That's the main thing I want to know.
i use this approach for a table called call, with a column of timestamp type called systemdate:
SELECT * FROM `call` WHERE DATE(`systemdate`) = DATE(NOW());
mysql DATE() statement gets the datepart of a datetime or timestamp field.
Sorry, just noticed that you want to check if atleast there is an entry for each of the days in the previous week.
you can use this query to check the prevous days individually:
yesterday:
SELECT * FROM `call` WHERE DATE(`systemdate`) = DATE(NOW()) - 1;
before yesterday:
SELECT * FROM `call` WHERE DATE(`systemdate`) = DATE(NOW()) - 2;
Or you can check the whole week at once:
SELECT * FROM `call` WHERE DATE(`systemdate`) > DATE_SUB(CURDATE(), INTERVAL 7 DAY) GROUP BY DATE(`systemdate`);
This will return one result for each day, so if you have 7 results you'll know at least an entry was made on each day.
select * from table
where created between subdate(current_date, interval 7 day) and current_date;
Selecting datetimes up to current_date includes everything up to the start of "today" (ie "the previous midnight").
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