Best way to store time (hh:mm) in a database(在数据库中存储时间 (hh:mm) 的最佳方式)
问题描述
我想将时间存储在数据库表中,但只需要存储小时和分钟.我知道我可以只使用 DATETIME 并忽略日期的其他组成部分,但是在不存储比我实际需要的更多信息的情况下执行此操作的最佳方法是什么?
I want to store times in a database table but only need to store the hours and minutes. I know I could just use DATETIME and ignore the other components of the date, but what's the best way to do this without storing more info than I actually need?
推荐答案
您可以将其存储为午夜过后分钟数的整数:
You could store it as an integer of the number of minutes past midnight:
例如.
0 = 00:00
60 = 01:00
252 = 04:12
然而,您需要编写一些代码来重构时间,但这应该不难.
You would however need to write some code to reconstitute the time, but that shouldn't be tricky.
这篇关于在数据库中存储时间 (hh:mm) 的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在数据库中存储时间 (hh:mm) 的最佳方式
基础教程推荐
- 从字符串 TSQL 中获取数字 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
