一、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
二、MySQL中兩個Time字段相減
如果兩個字段都為Time類型,如果兩個時間都在同一天,相減可以得到相差的秒數,但如果跨天,月,年都有問題。
selec t (TIME_TO_SEC(endDateTime) - TIME_TO_SEC(beginDateTime)) dif_second from tblName
轉載自:http://www.sowsoy.com/topics-577.html,感謝分享!