mysql TIMESTAMPDIFF 函数用法
时间差函数:TIMESTAMPDIFF
MySQL
语法: TIMESTAMPDIFF (unit,begin,end)
begin和end可以为DATE或DATETIME类型,并且可允许参数为混合类型。
unit参数是确定(end-begin)时间差的单位,单位值如下表
单位 | MySQL(UNIT) | DB2(UNIT) |
毫秒 | 1 | |
秒 | second | 2 |
分钟 | minute | 4 |
小时 | hour | 8 |
天 | day | 16 |
周 | week | 32 |
月 | month | 64 |
季 | quarter | 128 |
年 | year | 256 |
注意 ,当传入混合参数时end-begin 不足 1个unit的时候 为0.
例如
SELECT TIMESTAMPDIFF (day,"2022-10-19 09:43:21","2022-10-20 00:00:00"); 值为0
SELECT TIMESTAMPDIFF (day,"2022-10-19 09:43:21","2022-10-20"); 值为0
SELECT TIMESTAMPDIFF (day,"2022-10-19","2022-10-20"); 值为1
SELECT TIMESTAMPDIFF (day,"2022-10-19 09:43:21","2022-10-19 00:00:00"); 值为0
下班记得打卡