MySQL timestampdiff()函数返回两个时间之间的差值

TIMESTAMPDIFF(unit,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 timestampdiff(year, "2019-08-13 19:15:16","2020-08-14 13:47:37") as year
      ,timestampdiff(QUARTER, "2019-08-13 19:15:16","2020-08-14 13:47:37") as QUARTER
      ,timestampdiff(MONTH, "2020-07-14 13:47:37","2020-08-14 13:47:37") as MONTH
	  ,timestampdiff(WEEK , "2020-08-07 13:47:37","2020-08-14 13:47:37") as WEEK 
	  ,timestampdiff(DAY, "2020-08-13 13:47:37","2020-08-14 13:47:37") as DAY
	  ,timestampdiff(HOUR, "2020-08-14 12:47:37","2020-08-14 13:47:37") as HOUR
	  ,timestampdiff(MINUTE , "2020-08-14 12:47:37","2020-08-14 13:47:37") as MINUTE 
	  ,timestampdiff(SECOND , "2020-08-14 13:46:37","2020-08-14 13:47:37") as SECOND 

 

你可能感兴趣的:(Mysql,sql,timestampdiff)