mysql时间相减的问题

MySQL中时间不能直接相减,如果日、分、时不同,相减结果是错误的

mysql> select t1,t2,t2-t1 from mytest;

+---------------------+---------------------+-------+

| t1                  | t2                  | t2-t1 |

+---------------------+---------------------+-------+

| 2013-04-21 16:59:33 | 2013-04-21 16:59:43 |    10 |

| 2013-04-21 16:59:33 | 2013-04-21 17:00:33 |  4100 |

| 2013-04-21 16:59:33 | 2013-04-21 17:59:35 | 10002 |

+---------------------+---------------------+-------+

实际是mysql的时间相减是做了一个隐式转换操作,将时间转换为整数,但并不是用unix_timestamp转换,而是直接把年月日时分秒拼起来,如2013-04-21 16:59:33 直接转换为20130421165933,由于时间不是十进制,所以最后得到的结果没有意义,这也是导致上面出现坑爹的结果。

要得到正确的时间相减秒值,有以下3种方法:

1、time_to_sec(timediff(t2, t1)),

2、timestampdiff(second, t1, t2),

3、unix_timestamp(t2) -unix_timestamp(t1)

时间: 2024-07-30 15:46:48

mysql时间相减的问题的相关文章

asp.net(C#)时间相减 得到天数、小时、分钟、秒差

asp.net(C#)时间相减 得到天数.小时.分钟.秒差 DateTime dtone = Convert.ToDateTime("2007-1-1 05:00:00"); DateTime dtwo = Convert.ToDateTime("2007-1-5 08:00:00"); TimeSpan span = dtone.Subtract(dtwo); //算法是dtone 减去 dtwo tss.Text = span.Days + "天&qu

oracle 两个时间相减

oracle 两个时间相减默认的是天数 oracle 两个时间相减默认的是天数*24 为相差的小时数 oracle 两个时间相减默认的是天数*24*60 为相差的分钟数 oracle 两个时间相减默认的是天数*24*60*60 为相差的秒数 --MONTHS_BETWEEN(date2,date1) 给出date2-date1的月份 SQL> select months_between('19-12月-1999','19-3月-1999') mon_between from dual; MON_

Oracle 时间相减得出毫秒、秒、分、时、天,,【转】

http://blog.csdn.net/redarmy_chen/article/details/7351410 oracle 两个时间相减默认的是天数 oracle 两个时间相减默认的是天数*24 为相差的小时数 oracle 两个时间相减默认的是天数*24*60 为相差的分钟数 oracle 两个时间相减默认的是天数*24*60*60 为相差的秒数 --MONTHS_BETWEEN(date2,date1) 给出date2-date1的月份 SQL> select months_betwe

C#-和时间有关的计算代码、时间相减 得到天数、小时、分钟、秒差

asp.net(C#)时间相减 得到天数.小时.分钟.秒差 asp.net(C#)时间相减 得到天数.小时.分钟.秒差 DateTime dtone = Convert.ToDateTime("2007-1-1 05:00:00");         DateTime dtwo = Convert.ToDateTime("2007-1-5 08:00:00");         TimeSpan span = dtone.Subtract(dtwo); //算法是d

C#两个时间相减

原文地址:http://www.jb51.net/article/60177.htm using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { class Program { static void Main(string[] args) { DateTime t1 = DateTime.Parse("2007-01-01"); DateT

两个时间相减

<1> using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { class Program { static void Main(string[] args) { DateTime t1 = DateTime.Parse("2007-01-01"); DateTime t2 = DateTime.Parse("200

mysql 俩个时间相减后取分钟

CASE WHEN TIMESTAMPDIFF(MINUTE,o.createDate,o.chargingStartDate) !=15 THEN 'APP解锁计费' ELSE '系统自动计费' END TIMESTAMPDIFF(MINUTE,o.createDate,o.chargingStartDate) o.chargingStartDate减去o.createDate相减后得到分钟,如果后者小于前者分钟为负数. SELECT TIMESTAMPDIFF(MONTH,'2009-10-

oracle两时间相减得到相差的时间

1.months_between(date1,date2);date1和date2相减得到相差的月份. select months_between(to_date('2015-05-11','yyyy-MM-dd'),to_date('2015-04-11','yyyy-MM-dd')) from dual :相差一个月. 2.ceil(date1-date2);date1-date2相减得到相差的天数 select ceil(To_date('2015-05-11 00:00:00' , 'y

【转】实现Sqlite datediff日期时间相减的方法

对sqlite既恨又爱,在小项目里,用sqlite开发起来非常方便,效率足够用.美中不足的是sqlite精简了些功能及相关函数.如存储过程,datediff,不支持并发写入及远程访问(或许有些高手说这个不是问题,用共享或iis都可以解决,但这算真正解决吗?),导致在开发过程中走了不少弯路. 最常用到的日期及时间比较函数datediff,在sqlite里被精简了,网上也找不到详细的的实现方法.今天就花了点时间来自己实现datediff功能. 折腾一段时间后发现sqlite里有个julianday函