The use of Timestampdiff and Timestampadd functions in MySQL

Source: Internet
Author: User

Today's use of the Timestampdiff () function in the project requires the processing of a single piece that scans the scan time to a current time greater than 24 hours. The following values summarize the usage of the Timestampdiff and Timestampadd functions.

These two functions, Timestampdiff and timestampadd, are often used in MySQL applications.

One, Timestampdiff

Grammar:

Timestampdiff (INTERVAL,DATETIME_EXPR1,DATETIME_EXPR2).

Description

Returns an integer difference between a date or datetime expression Datetime_expr1 and datetime_expr2the. The unit of the result is given by the interval parameter. The parameter must be one of the following values:

    1. Frac_second. Indicates that the interval is milliseconds
    2. SECOND. Seconds
    3. MINUTE. Minutes
    4. HOUR. Hours
    5. Day. Days
    6. WEEK. Week
    7. MONTH. Month
    8. QUARTER. Quarter
    9. Year. Years
Use the following:
  1. mysql> Select timestampdiff (Day,' 2012-08-24 ',' 2012-08-30 '  );
  2. +----------------------------------------------+ 
  3. |  Timestampdiff (Day,' 2012-08-24 ',' 2012-08-30 ') |
  4. +----------------------------------------------+ 
  5. | 6 |
  6. +----------------------------------------------+ 
  7. 1 row in Set (0.00 sec)
  1. mysql> Select timestampdiff (MINUTE,' 2012-08-24 09:00:00 ','  2012-08-30 12:00:00 ');
  2. +-------------------------------------------------------------------+ 
  3. | Timestampdiff (MINUTE,' 2012-08-24 09:00:00 ',' 2012-08-30 12:00:00 '  ) |
  4. +-------------------------------------------------------------------+ 
  5. | 8820 |
  6. +-------------------------------------------------------------------+ 
  7. 1 row in Set (0.01 sec)
Two, Timestampadd syntax: Timestampadd (interval,int_expr,datetime_expr) Description: Adds an integer expression int_expr to a date or datetime expression datetime_expr. The interval in the formula are the same as the values listed above.
  1. mysql> Select timestampadd (MINUTE, 8820,' 2012-08-24 09:00:00 ');
  2. +-------------------------------------------------+ 
  3. |  Timestampadd (MINUTE, 8820,' 2012-08-24 09:00:00 ') |
  4. +-------------------------------------------------+ 
  5. | 2012-08-30 12:00:00 |
  6. +-------------------------------------------------+ 
  7. 1 row in Set (0.00 sec)

The use of Timestampdiff and Timestampadd functions in MySQL

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.