mysql 相同字段相减_mysql datetime 类型字段相减

背景:今天测试一个mariadb中datatime类型的字段相减问题,直接用2个字段相减得到的数值并不是秒,很坑。

后面百度了一些方法,说是使用 DateDiff 函数,这个函数文档还说有3个参数,但是3个参数运行是报错的。要疯了。最后放弃这个方法。

继续百度,终于找到一个靠谱的说法:DateDiff 函数2个值是可以的,再利用time_to_sec 函数转化为秒。

使用 DateDiff 函数时候的示例和报错:

select DateDiff('h',create_date,last_operate_date) as a,count(*) from bug_problem where create_date

between '2018-03-01' and '2018-03-27' and problem_status=6 group by a ;

d79beb6434ee

报错.png

下面使用 time_to_sec 转换:

示例:

select create_date,last_operate_date, time_to_sec(timediff(last_operate_date, create_date))/3600 as a

from bug_problem

where create_date between '2018-03-01' and '2018-03-27' and problem_status=6

and (time_to_sec(timediff(last_operate_date, create_date))/3600) >= 24

and (time_to_sec(timediff(last_operate_date, create_date))/3600) <= 120;

运行成功了:

d79beb6434ee

成功.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值