您当前的位置: 首页 >  sql

小志的博客

暂无认证

  • 3浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

求mysql中2个时间的时间差值

小志的博客 发布时间:2018-08-24 16:58:46 ,浏览量:3

1 求mysql中2个时间的时间差

select times,TIMESTAMPDIFF(YEAR,now(),times) as years from user 



当now()为2018-01-22 16:02:04时间,times为 2016-08-22 16:02:04

查出的结果集为 
------------------------------------------------
     times                          years
------------------------------------------------
 2018-01-22 16:02:04                  -2
------------------------------------------------

可以看出年份相差-2年
  •  

其中YEAR可以换成月份,秒 ,天

2 求绝对值

在以上查询中加入ABS()函数求绝对值

select times,ABS(TIMESTAMPDIFF(YEAR,now(),times)) as years from user 



当now()为2018-01-22 16:02:04时间,times为 2016-08-22 16:02:04

查出的结果集为 
------------------------------------------------
     times                          years
------------------------------------------------
 2018-01-22 16:02:04                 2
------------------------------------------------

可以看出年份相差2年
关注
打赏
1661269038
查看更多评论
立即登录/注册

微信扫码登录

0.1444s