MySQL中兩個DateTime字段相減
假定表名為tblName,兩個DateTime字段名分別為beginDateTime,endDateTime,以下是相關兩個mysql日期字段相減的SQL語句,這種方式兩字段跨天,月,年都無問題。
得到兩個日期字段之間的秒數
selec t (UNIX_TIMESTAMP(endDateTime) - UNIX_TIMESTAMP(beginDateTime)) dif_second from tblName
得到兩個日期字段之間的分數
selec t (UNIX_TIMESTAMP(endDateTime) - UNIX_TIMESTAMP(beginDateTime))/60 dif_minute from tblName
得到兩個日期字段之間的天數
selec t (UNIX_TIMESTAMP(endDateTime) - UNIX_TIMESTAMP(beginDateTime))/(60*60*24) dif_minute from tblName
出處:https://blog.csdn.net/cqrf2006/article/details/42080559
