简体   繁体   中英

MySQL: Calculate width date in column

I want to define a cron that freqently delete specific data from a MySQL column if date in date_added column is older than 60 days, but don't find any infomation if MySQL supports calculation width date. Is that possible and if yes how to do?

DELETE FROM 
vcount 
WHERE date_added - 60 days

date_added == datetime == 0000-00-00 00:00:00

assuming your date_added is a valid date column you can use date_sub

DELETE FROM 
vcount 
WHERE date_sub(date_added, INTERVAL 60 DAY);

You can use either date_add() or date_sub() functions:

where date_added < date_add(curdate(),INTERVAL -60 DAY)

or

where date_added < date_sub(curdate(),INTERVAL 60 DAY)

(many prefer to always use date_add() using a negative interval for subtraction)

use DATE_SUB(date, INTERVAL value interval)

date : Required. The date to be modified

value : Required. The value of the time/date interval to subtract. Both positive and negative values are allowed

interval: Required. The type of interval to subtract. Can be one of the following

values:

MICROSECOND

SECOND

MINUTE

HOUR

DAY

WEEK

MONTH

QUARTER

YEAR

SECOND_MICROSECOND

MINUTE_MICROSECOND

MINUTE_SECOND

HOUR_MICROSECOND

HOUR_SECOND

HOUR_MINUTE

DAY_MICROSECOND

DAY_SECOND

DAY_MINUTE

DAY_HOUR

YEAR_MONTH

DELETE FROM 
vcount 
WHERE date_added  < (DATE_SUB(NOW(), INTERVAL 60 DAY))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM