MySQL的时间差函数TIMESTAMPDIFF、DATEDIFF的用法
datediff函数,返回值是相差的天数,不能定位到小时、分钟和秒。
select datediff('2020-5-1 09:00:00', '2020-5-8 07:00:00');
TIMESTAMPDIFF: 可以进行参数设置,精确到天(DAY)、小时(HOUR),分钟(MINUTE)和秒(SECOND)
SQL写法:
求两者之间的天数 select TIMESTAMPDIFF(DAY, '2020-5-1 09:00:00', '2020-5-8 07:00:00') -- 6 求两者之间的小时 select TIMESTAMPDIFF(HOUR, '2020-5-1 09:00:00', '2020-5-8 07:00:00') 求两者之间的分钟 select TIMESTAMPDIFF(MINUTE, '2020-5-1 09:00:00', '2020-5-8 07:00:00') 求两者之间的秒 select TIMESTAMPDIFF(SECOND, '2020-5-1 09:00:00', '2020-5-8 07:00:00')
-- 根据身份证号获取年龄
select TIMESTAMPDIFF( YEAR, substr( 省份证号, 7, 8 ), CURDATE( ) )