How to delete old data? - mysql

I have a lot of old data. And I want to delete it.
Data example:
id ,title, date
What is the best way to delete old data using SQL? I mean delete row if too old, for example 1 month. How can I compare row's date with current time?
PS. I have date column. Look at the example.

Try sommething like this:-
DELETE FROM myTable WHERE [columndate]< DATE_SUB(NOW(), INTERVAL 1 MONTH);

If there isn't any row which stores when was the data inserted then it cannot be specified.
If there is a row named createddate,
you can try
delete from mytable where DATEDIFF(createddate, now())>30
OR you can try with
delete from mytable where createddate< DATE_SUB(NOW(), INTERVAL 1 MONTH);

DELETE * FROM TableName WHERE date < DATE();

Related

How to adjust timestamp to yesterday date on MySQL database?

Can anybody please tell me how to add a yesterday time stamp whenever my table get updated? Currently, it giving me today date instead yesterday date. Please see below picture.
I tired adding (CURRENT_TIMESTAMP, -1) on default/Expression. Did not work.
Comment Picture
Thank you so much
You can use SUBDATE(CURRENT_DATE, INTERVAL 1 DAY) to subtract one day from today's date. For example:
SELECT *
FROM table_name
WHERE DATE(Date) = SUBDATE(CURRENT_DATE(), INTERVAL 1 DAY)
UPDATE
To update the value in the database, you can do the following:
UPDATE table_name
SET date = SUBDATE(CURRENT_DATE(), INTERVAL 1 DAY)
WHERE column = 'value'
I also needed the same, to update yesterday date whenever my table gets data. I have scheduled the following query, which worked perfectly for me.
I hope it will work for you also:
sql_yesterday_date = 'UPDATE tbl_dailytrade SET Date = subdate(current_date, 1) WHERE Date(date) = CURDATE()'

Truncate table with date time older than n days

I want to truncate some records from a table with date time clause.
This link have a solution, but it's not working for me.
TRUNCATE TABLE 'meter_to_sim_mapping' WHERE 'meter_to_sim_mapping'.'mapped_at' <=
In above where clause I want to add the date time value. The value is 2018-04-02 16:03:52. The query should delete all the records prior to this date.
How can I truncate the table with date-time?
Any help would be highly appreciated.
You need to use DELETE
DELETE FROM TABLE_NAME
WHERE DATE_COLUMN < NOW() - INTERVAL N DAY
or
DELETE FROM TABLE_NAME
WHERE CAST(DATE_COLUMN AS DATE) < STR_TO_DATE('1-01-2012', '%d-%m-%Y') - INTERVAL N DAY
In place of NOW() You can use your datetime value
STR_TO_DATE('12-01-2014 00:00:00','%m-%d-%Y %H:%i:%s')
Demo
http://sqlfiddle.com/#!9/4607a6/1
Below example may work for you:
DELETE FROM TABLE_NAME
WHERE DATE_COLUMN = DATE_SUB("2018-04-02 16:03:52", INTERVAL 10 DAY);
You can change it accordingly as per your requirement.
Reference: MySQL DATE_SUB() Function

Create delete event that deletes older records on daily basis

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

How to reduce one day in existing date column in mysql database?

I have table tbl_dtcount. In that table there is one column for date.
Now I need to reduce one day for each and every rows in that date field. The date is beginning from 2012-05-19 to 2012-07-03. What is the MySQL update statement to perform this?
How about this.
Update tbl_dtcount
set mydate = DATE_SUB(mydate, INTERVAL 1 DAY)
where <conditions>;
UPDATE table_name
SET date_column = DATE_SUB('1998-01-02', INTERVAL 1 DAY)
....
see detail MySQL DATE_SUB

How to Update all Dates in a Table

I have a table with 5 million DATETIME records. I want to add a year to all the various datetimes in the table. Can I do it all with a single query? Something like:
SELECT DATE_ADD(*, INTERVAL 1 YEAR);
Or any other way you would recommend. Thanks!
This should do what you want:
UPDATE table SET datefield = DATE_ADD(datefield, INTERVAL 1 YEAR);
If you need to update every table in the database check the answers to this question