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.