Mysql update time (plus or minus a period of time)

Mysql time addition and subtraction functions are date_add(), date_sub()
Definition and Usage
The DATE_ADD() function adds the specified interval to a date.
The DATE_SUB() function decrements a date by the specified interval.
grammar
DATE_ADD(date,INTERVAL expr type)
DATE_SUB(date,INTERVAL expr type)
 
The date parameter is a valid date expression.
The expr parameter is the time interval you wish to add.
The type parameter can have the following values:
Type value
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
 
1. MySQL adds a time interval to the date: date_add()
[html] view plain copy
  1. set @dt = now();
  2. select date_add(@dt, interval 1 day); - add 1 day
  3. select date_add(@dt, interval 1 hour); - add 1 hour
  4. select date_add(@dt, interval 1 minute); - add 1 minute
  5. select date_add(@dt, interval 1 second); - add 1 second
  6. select date_add(@dt, interval 1 microsecond); - add 1 millisecond
  7. select date_add(@dt, interval 1 week); - add 1 week
  8. select date_add(@dt, interval 1 month); - add 1 month
  9. select date_add(@dt, interval 1 quarter); - add 1 quarter
  10. select date_add(@dt, interval 1 year); - add 1 year
Example: Update a certain time, add one week to each time
[html] view plain copy
  1. UPDATE comment c set c.time = DATE_ADD(c.time, INTERVAL 7 DAY) ;
 
2. MySQL subtracts a time interval for the date: date_sub(), the format is similar to date_add()
Example: Update a certain time, reducing each time by one month
[html] view plain copy
  1. UPDATE comment c set c.time = DATE_SUB(c.time, INTERVAL 1 MONTH)
 
You can also use these functions when inserting data. Such as
update sql:
update v_po_part r
set r.otdStandardTime = date_add(now(),interval 1 DAY)
where r.id ='57'
Insert sql:
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325022892&siteId=291194637