问题
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:
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.
Is there any solution available for my problem? Am executing this insert query within a stored procedure.
回答1:
In MySQL,
you can store 80:00:00
for time
datatype.
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:
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. for example:
if you stored the 80 hours what mysql do is set current time + your input.
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.
SQL FIDDLE
回答2:
It seems that you have a problem from your sql client because of the nature of the exception "java.sql.SQLException".
- Go in in a bare console or another sqlclient and try to execute what you were trying to do
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. See doc for more
回答3:
I think I understand the use case, but I'm not sure I would elect to use the time datatype for the solution. It might as well be some sort of int datatype and named idle_time_in_seconds
You might also choose milliseconds if you need to be more exact.
来源:https://stackoverflow.com/questions/49915200/how-to-store-time-greater-than-24-hours-in-mysql