mysql中两个DateTime字段相减 | 您所在的位置:网站首页 › sql相减取绝对值 › mysql中两个DateTime字段相减 |
一、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 |
CopyRight 2018-2019 实验室设备网 版权所有 |