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
- set @dt = now();
- select date_add(@dt, interval 1 day); - add 1 day
- select date_add(@dt, interval 1 hour); - add 1 hour
- select date_add(@dt, interval 1 minute); - add 1 minute
- select date_add(@dt, interval 1 second); - add 1 second
- select date_add(@dt, interval 1 microsecond); - add 1 millisecond
- select date_add(@dt, interval 1 week); - add 1 week
- select date_add(@dt, interval 1 month); - add 1 month
- select date_add(@dt, interval 1 quarter); - add 1 quarter
- 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
- 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
- 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: