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

 

posted @ 2022-10-19 11:19  了悟  阅读(963)  评论(0编辑  收藏  举报