Datetime interval of mySQL

Source: Internet
Author: User

MySQL compares two datetime-type time intervals, in seconds:

SELECTTIME_TO_SEC (TIMEDIFF ('2017-02-09 11:24:46 ', '2017-02-09 10:23:46 '));

First read select timediff ('2017-02-09 11:24:46 ', '2017-02-09 10:23:46 ')
01:01:00
SELECT TIME_TO_SEC('01:01:00')
3660 
Therefore, we know the number of seconds between the two datetime data.
If we consider that the two datetime data are first converted to their respective seconds by TIME_TO_SEC and then subtracted, isn't the same effect?
SELECT TIME_TO_SEC('2009-02-09 11:24:46')-TIME_TO_SEC('2009-02-09 10:23:46')
3660
It seems that this method is also OK, but we will observe a group of data and you will know why we cannot use this method to compare the time interval between two datetime data.
Now we use '2017-02-0811:24:46 and '2017-02-09 10:23:46 'comparison:
SELECT TIME_TO_SEC('2009-02-08 11:24:46')-TIME_TO_SEC('2009-02-09 10:23:46')
3660
It's strange that the parameter on the left is one day earlier than the parameter on the right. Why is the number of seconds subtracted or a positive value?
Originally, TIME_TO_SEC only converts the time part of datetime data to the number of seconds, and does not care who is the size of the date, so to compare two datetime data, you must first convert TimeDiff to the number of seconds, start:
SELECT TIME_TO_SEC(TIMEDIFF('2009-02-09 11:24:46','2009-02-09 10:23:46'));

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.