TIMESTAMPDIFFunit,begin,end);
TIMESTAMPDIFF函数返回begin-end的结果,其中begin和end是DATE或DATETIME表达式。
TIMESTAMPDIFF函数允许其参数具有混合类型,例如,begin是DATE值,end可以是DATETIME值。 如果使用DATE值,则TIMESTAMPDIFF函数将其视为时间部分为“00:00:00”的DATETIME值。
unit参数是确定end-begin)的结果的单位,表示为整数。 以下是有效单位:
MICROSECOND 微秒 SECOND 秒 MINUTE 分钟 HOUR 小时 DAY 天 WEEK 周 MONTH 月份 QUARTER 季度 YEAR 年份
示例:
select timestampdiffyear, "2019-08-13 19:15:16","2020-08-14 13:47:37") as year
,timestampdiffQUARTER, "2019-08-13 19:15:16","2020-08-14 13:47:37") as QUARTER
,timestampdiffMONTH, "2020-07-14 13:47:37","2020-08-14 13:47:37") as MONTH
,timestampdiffWEEK , "2020-08-07 13:47:37","2020-08-14 13:47:37") as WEEK
,timestampdiffDAY, "2020-08-13 13:47:37","2020-08-14 13:47:37") as DAY
,timestampdiffHOUR, "2020-08-14 12:47:37","2020-08-14 13:47:37") as HOUR
,timestampdiffMINUTE , "2020-08-14 12:47:37","2020-08-14 13:47:37") as MINUTE
,timestampdiffSECOND , "2020-08-14 13:46:37","2020-08-14 13:47:37") as SECOND