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

Source: Internet
Author: User
Tags mysql update

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
    1. Set @dt = Now ();
    2. Select Date_add (@dt, Interval 1 day); -Plus 1 days
    3. Select Date_add (@dt, interval 1 hour); -Plus 1 hours
    4. Select Date_add (@dt, interval 1 minute); -Plus 1 minutes
    5. Select Date_add (@dt, Interval 1 second); -Plus 1 seconds
    6. Select Date_add (@dt, interval 1 microsecond);-plus 1 milliseconds
    7. Select Date_add (@dt, Interval 1 week);-Plus 1 weeks
    8. Select Date_add (@dt, interval 1 month);-Plus January
    9. Select Date_add (@dt, interval 1 quarter);-Plus 1 seasons
    10. Select Date_add (@dt, interval 1 year);-Plus 1 years
Example: Update a time, plus one weeks per time [HTML] view plain copy
    1. 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
    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 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.