The MySQL time plus function defines and uses the Date_add (), Date_sub () () function to add a specified time interval to a date. The Date_sub () function reduces the specified time interval to a date. The syntax Date_add (date,interval expr type) date_sub (Date,interval expr type) is a valid date expression. The expr parameter is the time interval that you want to add. The type parameter can be the following value:
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 for the date: Date_add () [HTML] View plain copy
- Set @dt = Now ();
- Select Date_add (@dt, Interval 1 day); -Plus 1 days
- Select Date_add (@dt, interval 1 hour); -Plus 1 hours
- Select Date_add (@dt, interval 1 minute); -Plus 1 minutes
- Select Date_add (@dt, Interval 1 second); -Plus 1 seconds
- Select Date_add (@dt, interval 1 microsecond);-plus 1 milliseconds
- Select Date_add (@dt, Interval 1 week);-Plus 1 weeks
- Select Date_add (@dt, interval 1 month);-Plus January
- Select Date_add (@dt, interval 1 quarter);-Plus 1 seasons
- Select Date_add (@dt, interval 1 year);-Plus 1 years
Example: Update a time, plus one weeks per time [HTML] view plain copy
- UPDATE Comment C Set c.time = Date_add (C.time, INTERVAL 7 day);
2. MySQL for date minus one time interval: Date_sub (), formatted with Date_add () Similar example: Update a time to reduce the time by one months [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 updating SQL:
Update V_po_part R
Set r.otdstandardtime = Date_add (now (), Interval 1 day)
where r.id = ' $ '
Insert sql:
Mysql Update time (plus or minus a period of time)