简体   繁体   English

如何在Mysql中存储大于24小时的时间?

[英]How to store time greater than 24 Hours in Mysql?

I need to create a column (say idletime ) that will store time value having hours smaller or bigger than 24. When i try to insert such values bigger than 24 (For example '80:00:00', '129:23:12', etc) , am getting the following error code: 我需要创建一个列(例如idletime ),该列将存储小时值小于或大于24的时间值。当我尝试插入大于24的值时(例如'80:00:00','129:23:12 '等),正在获取以下错误代码:

java.sql.SQLException: Illegal hour value '80' for java.sql.Time type in value '80:00:00.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
.....

I have searched over the Internet and found that the TIME datatype ranges from 00:00:00 to 23:59:59 and does not reach 24:00:00. 我已经在Internet上搜索了,发现TIME数据类型的范围是从00:00:00到23:59:59,并且没有达到24:00:00。

Is there any solution available for my problem? 有什么解决方案可以解决我的问题吗? Am executing this insert query within a stored procedure. 正在存储过程中执行此插入查询。

It seems that you have a problem from your sql client because of the nature of the exception " java.sql.SQLException ". 由于异常“ java.sql.SQLException ”的性质,看来您的sql客户端有问题。

  • Go in in a bare console or another sqlclient and try to execute what you were trying to do 进入裸机控制台或其他sqlclient并尝试执行您尝试执行的操作

for example: 例如:

CREATE Table mytimetable (testtime TIME);

Insert into mytimetable values ('1:1:1'),('080:1:1'),('80:1:1');

select * from mytimetable ;
01:01:01
80:01:01
80:01:01

MySQL uses the 'HHH:MM:SS' format, which is larger than 24 hours. MySQL使用'HHH:MM:SS'格式,该格式大于24小时。 See doc for more 有关更多信息,请参见文档

In MySQL, you can store 80:00:00 for time datatype. 在MySQL中,您可以为time数据类型存储80:00:00

CREATE TABLE `test_time` (
  `id` int(11) NOT NULL,
  `time` time DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

When getting the datetime with QUERY: 使用QUERY获取日期时间时:

SELECT TIMESTAMP(`time`) FROM `test_time`

Output: 输出:

TIMESTAMP(`time`)    
---------------------
2018-04-22T08:00:00Z 

What i noticed is when storing in above format, it will +80 hours in stored time. 我注意到的是以上述格式存储时,存储时间将增加80小时。 for example: 例如:

if you stored the 80 hours what mysql do is set current time + your input. 如果您存储了80小时,那么mysql会设置当前时间+您的输入。 when you want to check what date and time it would be when adding 80:00:00 it will show above output, Also it can store max 838:59:59 hours. 当您想查看添加80:00:00日期和时间时,它将显示在输出上方,并且它最多可以存储838:59:59小时。

SQL FIDDLE SQL字段

I think I understand the use case, but I'm not sure I would elect to use the time datatype for the solution. 我想我了解用例,但是我不确定我会选择使用time数据类型作为解决方案。 It might as well be some sort of int datatype and named idle_time_in_seconds 也可能是某种int数据类型,并命名为idle_time_in_seconds

You might also choose milliseconds if you need to be more exact. 如果需要更精确的话,也可以选择毫秒。

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

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