MySQL 存储持续时间 - 数据类型? [英] MySQL storing duration time - datatype?

查看:56
本文介绍了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 存储持续时间 - 数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆