MySQL calculates the difference in days between two dates

MySQL calculates the number of days between two dates
1. Use the TO_DAYS function
select to_days(now()) - to_days('20120512')

2. Use the DATEDIFF function
select datediff(now(),'20120512')
The TIMESTAMPDIFF function
can be used to calculate the difference between two dates in days, seconds, minutes, weeks, hours Syntax
: TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2 )
unit has the following types: FRAC_SECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR
- For example:
# Calculate the number of seconds between two dates
select timestampdiff(SECOND,'1997-10- 12','2022-01-10');
# Calculate the number of days between the two dates
select timestampdiff(DAY,'1997-10-12','2022-01-10');
# Calculate the length of service
SELECT TIMESTAMPDIFF(YEAR ,'2019-07-20',NOW()) AS 'working years';

Guess you like

Origin blog.csdn.net/qq_43632987/article/details/127551486