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.
相关文章