天數時間差:
select datediff('2018-07-09','2018-06-05'); --34
月數時間差:
select months_between('1997-02-28', '1996-1-30'); --12.93548387
年數時間差:
其他方式:
先轉換為時間戳格式再求時間差:
将兩個字段轉換為為毫秒類型時間戳,相減,再轉換為2019-06-23 00:00:00 這種形式并取兩位小數點
如果時間差機關為天的話則除以3600*24 如果時間差機關是小時的話則除以3600
示例:
時間格式為2019-06-23 00:00:00
計算天數時間差:
select round(((unix_timestamp('2019-06-23 00:00:00') - unix_timestamp('2019-06-22 00:00:00') ) / (3600*24)),2) hktime
計算小時時間差:
select round(((unix_timestamp('2019-06-23 00:00:00') - unix_timestamp('2019-06-22 00:00:00') ) / (3600)),2) hktime
計算分鐘時間差:
select round(((unix_timestamp('2019-06-23 00:00:00') - unix_timestamp('2019-06-22 00:00:00') ) / 60),2) hktime
計算秒時間差:
select round(((unix_timestamp('2019-06-23 00:00:00') - unix_timestamp('2019-06-22 00:00:00') )),2) hktime