How to subtract two date formats in mysql _ MySQL

Source: Internet
Author: User
Mysql sets two date formats to subtract. mysql sets two date formats to Subtract:


Original data table data:



Select (atime-btime) sec from data table;

The subtraction result is not a second. pay special attention to it!

Result:




To calculate the time interval (in seconds) between two Datetime types in mysql, you need to convert:
1. there is no problem across days, months, or years
Select (UNIX_TIMESTAMP (atime)-UNIX_TIMESTAMP (btime) sec

From data table;





2. it can only be used in Time type, cross-day, month, or year.
Select (TIME_TO_SEC (atime)-TIME_TO_SEC (btime) sec

From data table;

Select (TIME_TO_SEC (end_time)-TIME_TO_SEC (start_time) sec from task_detail where end_time is not null;

The results are the same;


Modify data:


Update task_detail set end_time = now () where id = 8;


The query result is a negative value. for details, see:




Therefore, when we subtract two dates, we use(TIME_TO_SEC (atime)-TIME_TO_SEC (btime) secThe method is better.

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.