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
UPDATEwill be clean and simple - i.e.duration = duration + $increment - As Tristram noted, there are limitations to using the
TIMEfield - e.g. "TIMEvalues 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 存储持续时间 - 数据类型?
基础教程推荐
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 在多列上分布任意行 2021-01-01
