MySQL storing duration time - datatype?(MySQL 存储持续时间 - 数据类型?)
问题描述
我需要计算用户在网站上花费的时间.注销时间和登录时间之间的区别是给我一些类似X 先生在网上花了 4 小时 43 分钟"的信息.所以为了存储 4 小时 43 分钟,我是这样声明的:duration
时间非空
I need to calculate the time a user spends on site. It is difference between logout time and login time to give me something like "Mr X spent 4 hours and 43 minutes online". So to store the4 hours and 43 minutes i declared it like this:
duration
time NOT NULL
这是有效的还是更好的存储方式?我需要存储在数据库中,因为我还有其他计算需要将其用于 + 其他用例.
Is this valid or a better way to store this? I need to store in the DB because I have other calculations I need to use this for + other use cases.
推荐答案
将其存储为整数秒将是最好的方法.
Storing it as an integer number of seconds will be the best way to go.
UPDATE
将简洁明了 - 即duration = duration + $increment
- 正如 Tristram 指出的那样,使用
TIME
字段 - 例如"TIME
值的范围可以从'-838:59:59'
到'838:59:59'
" - 不会对天/小时/分/秒显示格式进行硬编码.
- 使用整数秒数"字段时,其他计算的执行几乎肯定会更清晰.
- The
UPDATE
will be clean and simple - i.e.duration = duration + $increment
- As Tristram noted, there are limitations to using the
TIME
field - e.g. "TIME
values may range from'-838:59:59'
to'838:59:59'
" - The days/hours/minutes/seconds display formatting won't be hardcoded.
- The execution of your other calculations will almost surely be clearer when working with an integer "number of seconds" field.
这篇关于MySQL 存储持续时间 - 数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL 存储持续时间 - 数据类型?


- SQL 临时表问题 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 更改自动增量起始编号? 2021-01-01