简体   繁体   English

计算MySQL表中两个时间字段之间的时差

[英]Calculate difference between two time fields in MySQL table

I want to calculate difference between two time fields in a view (mysql). 我想计算视图中两个时间字段之间的差异(mysql)。 I'm using this query: 我正在使用此查询:

$result = mysql_query("CREATE OR REPLACE VIEW arbtim AS SELECT week, name, start, stop, arton,
      ( arton ) - ( start ) AS t18 FROM arbtid;");

The field "arton" is always "18:00:00". “ arton”字段始终为“ 18:00:00”。 If start = "10:00:00" it returns "80000". 如果开始=“ 10:00:00”,则返回“ 80000”。 If start = "09:45:00" it returns "85500". 如果开始=“ 09:45:00”,则返回“ 85500”。 I want it to be "8" and 8,25 我希望它是“ 8”和8,25

Both fields are in time format. 两个字段均为时间格式。 The created field t18 is in the double format - why? 创建的字段t18为双精度格式-为什么?

Reading the documentation ( http://dev.mysql.com/doc/refman/5.7/en/date-and-time-type-conversion.html ), unfortunately 85500 is the documented behaviour. 阅读文档( http://dev.mysql.com/doc/refman/5.7/en/date-and-time-type-conversion.html )时,不幸的是记录的行为是85500。 I know what you are thinking... that sucks :( 我知道你在想什么...

The problem is that TIME is converted to 10000 * hours + 100 * minutes + seconds. 问题是TIME转换为10000 *小时+ 100 *分钟+秒。

Since you want hours, I suggest you do it explicitly: 由于您需要几个小时,因此建议您明确地执行以下操作:

(3600*HOUR(arton)+60*MINUTE(arton)+SECOND(arton)
-3600*HOUR(start)-60*MINUTE(start)-SECOND(start))/3600 AS t18

which (thanks to GolezTrol), with the TIME_TO_SEC function, can be more simply written as: 借助TIME_TO_SEC函数,(借助GolezTrol)可以更简单地编写为:

(TIME_TO_SEC(arton)-TIME_TO_SEC(start))/3600 AS t18

But I want to go deeper with this answer, because this strange TIME algebra bothers me a lot. 但是我想更深入地了解这个答案,因为这个奇怪的TIME代数让我很困扰。 I've never seen something like this in any other RDBMS or language. 我从未在任何其他RDBMS或语言中看到过类似的东西。 Mind you, I haven't seen them all, but quite a few. 请注意,我还没有全部看到,但有很多。

What bothers me is that this TIME algebra is a trap, especially for the inexperienced. 令我困扰的是,这个TIME代数是一个陷阱,特别是对于没有经验的人。

There are just too many things that can go wrong with this. 有太多事情可能会出错。 Here's a partial list: 以下是部分列表:

  • 100 is 00:01:00, but 100-1 is not 00:00:59, but an invalid time 100是00:01:00,但100-1不是00:00:59,而是无效时间
  • 100 is 00:01:00, but 100*100 is not 01:40:00, but 01:00:00 100是00:01:00,但100 * 100不是01:40:00,而是01:00:00
  • 100 is 00:01:00, but 100/2 is not 00:00:30, but 00:00:50 100是00:01:00,但100/2不是00:00:30,而是00:00:50

... and many others I might add as I think of them. ...以及我想起的其他许多内容。

Do you see where I am getting? 你知道我要去哪里吗? Do not operate on time columns... not in this warped and incomplete space MySQL provides us. 不要在时间列上进行操作……不要在这个扭曲和不完整的空间中MySQL提供给我们。 In this space most operations will not work as we would expect them to. 在这个空间中,大多数操作将无法正常运行。

The only reasonable solution here is to get out... convert those times to something else we can sum, subtract, divide and multiply as we would expect. 这里唯一合理的解决方案是摆脱困境...将那些时间转换为我们可以期望的加,减,除和乘的其他时间。 TIME_TO_SEC is a good simple choice, but there are others. TIME_TO_SEC是一个很好的简单选择,但还有其他选择。 And do convert TIME values before doing anything else with them. 对它们进行任何其他操作之前先转换TIME值。

There are a couple of correct ways of doing it: 有两种正确的方法:

Using timestamps - in this case, the result will be in seconds: 使用时间戳-在这种情况下,结果将以秒为单位:

SELECT week, name, start, stop, arton,  UNIX_TIMESTAMP( arton ) - UNIX_TIMESTAMP( start ) AS t18 FROM arbtid;

Then, to get hours, divide the number of seconds by 3,600. 然后,要获得小时数,请用秒数除以3600。

Or using TIMEDIFF - in this case, the result will be a time value like "-00:00:00.000001": 或使用TIMEDIFF在这种情况下,结果将是类似于“ -00:00:00.000001”的时间值:

SELECT week, name, start, stop, arton,  TIMEDIFF( arton, start ) AS t18 FROM arbtid;

I think the former is probably more suitable for your application. 我认为前者可能更适合您的应用程序。

You can use TIME_TO_SEC to get the number of seconds in the time span. 您可以使用TIME_TO_SEC来获取时间跨度中的秒数。 Then, you can use that to calculate the hours, by simply dividing it by 3600. 然后,您可以通过简单地将其除以3600来计算小时数。

SELECT TIME_TO_SEC(arton - start) / 3600 AS HoursBetween FROM arbtid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM