简体   繁体   English

如何创建一个存储时间(小时和分钟)的mysql表?

[英]How to create a mysql table that stores time (hours and minutes)?

I'd like to create a mysql table that stores time (hours and minutes), such as 456:45 (456 hours and 45 minutes) 我想创建一个存储时间(小时和分钟)的mysql表,例如456:45(456小时45分钟)

CREATE TABLE mytable(id VARCHAR(40) NOT NULL, my_time DATE NOT NULL); 创建表mytable(id VARCHAR(40)NOT NULL,my_time DATE NOT NULL);

which data type should be used? 应该使用哪种数据类型?

If the TIME datatype range is enough for you, go for it. 如果TIME数据类型范围足以满足您的要求,那就去吧。 It stores information with a higher precision than you probably need, and has some quirks: 它以比您可能需要的精度更高的精度存储信息,并且有一些怪癖:

out-of-range TIME values are clipped to the appropriate endpoint of the TIME range.

Otherwise, if you only need minutes, you can employ a DECIMAL datatype. 否则,如果只需要几分钟,则可以使用DECIMAL数据类型。 You have two approaches available: 您有两种方法可用:

  1. Decimals are minutes. 小数是分钟。 Therefore, 1200 hours and 30 minutes is 1200.30. 因此,1200小时30分钟为1200.30。

Advantages: you have full precision with two digits, and you can immediately read the minutes by truncating. 优点:您可以精确到两位数,并且可以通过截断立即读取分钟。

Disadvantages: 0.30 + 0.30 ought to give 1.00, not 0.60. 缺点:0.30 + 0.30应该给出1.00,而不是0.60。 Also, "One minute before 27:00" will be 26.99, not 26.59. 另外,“ 27:00之前一分钟”将是26.99,而不是26.59。

  1. Normalize to the hour. 归一化为小时。 That is, "1200.5" represents 1200 hours and a half, or 1200h 30'. 也就是说,“ 1200.5”代表1200小时半或1200h 30'。

Advantages: calculations become very simple. 优点:计算变得非常简单。

Disadvantages: it's not immediate to look at 17.25 and interpret it as 17 hours and 15 minutes. 缺点:查看17.25并将其解释为17小时15分钟并不是立即的。 You need to get the decimal part and multiply by 0.60. 您需要获取小数部分并乘以0.60。 Also, to have acceptable precision, you need more than two decimals (3 or 4 depending on conversion techniques). 另外,要获得可接受的精度,您需要两个以上的小数(3或4个小数,具体取决于转换技术)。

A further possibility is to do what MySQL does (albeit at the microsecond level) and use an INTEGER field to store the minutes. 另一种可能性是执行MySQL的操作(尽管处于微秒级别),并使用INTEGER字段存储分钟。 Then 1200 hours 30 minutes will be represented by the integer 72030. Divide by 60 to get the hours, do a modulo 60 to get the minutes. 然后,以整数72030表示1200小时30分钟。除以60得到小时,以60为模得到分钟。

TIME - displays values in HH:MM:SS TIME -以HH:MM:SS显示值

The range is '-838:59:59' to '838:59:59'. 范围是“ -838:59:59”至“ 838:59:59”。

You can assign values to time column using strings or numbers. 您可以使用字符串或数字将值分配给时间列。

1) Create VARCHAR column and when you save data use separator like :- 1)创建VARCHAR列,然后在保存数据时使用分隔符,如:-

456:45,456_45,456-45

2) Other better option is convert time in second or Create Int column and save time in seconds 2)其他更好的选择是convert time in second或创建Int列,并以seconds节省时间

The data type that should be used is: 应该使用的数据类型是:

DATETIME

OR take a look at this: 或看看这个:

http://www.w3schools.com/sql/sql_dates.asp

Hope this helps, 希望这可以帮助,

Sohail. Sohail。

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

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