MySQL Time subtraction problem

Source: Internet
Author: User

MySQL time can not be directly subtracted, if the day, minute, time difference, subtract the result is wrong

Mysql> select T1,t2,t2-t1 from MyTest; +---------------------+---------------------+-------+  | T1 | T2 |  T2-t1 | +---------------------+---------------------+-------+  | 2013-04-21 16:59:33 |    2013-04-21 16:59:43 |  10 | | 2013-04-21 16:59:33 |  2013-04-21 17:00:33 |  4100 | | 2013-04-21 16:59:33 | 2013-04-21 17:59:35 |  10002 | +---------------------+---------------------+-------+ The actual MySQL time subtraction is to do an implicit conversion operation, converting time to integers, but not with unix_timestamp conversion, but directly to the date and time of the day to spell up, such as 2013-04-21 16:59:33 Direct conversion to 20130421165933, because the time is not a decimal, so the final result is meaningless, which is also caused by the results of the pit father above. to get the correct time to subtract the second value, there are 3 ways to do this: 1, time_to_sec (Timediff (T2, T1)), 2, Timestampdiff (second, T1, T2), 3, Unix_timestamp (T2)-unix_timestamp (t1)

MySQL Time subtraction problem

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.