首页 > 解决方案 > 当数据为纪元时间格式时,如何使用 sql 查询获取特定时间范围?

问题描述

我想获取时间范围在上午 7 点到 12 点之间的数据,但数据是纪元时间格式。请帮我解决一下这个。

数据库如下所示:

ID Timestamp
1  1591622630224
2  1591622632812
3  1591622635282

标签: mysqlsql

解决方案


嗯。. . 您的 Unix 时间戳以毫秒而不是秒为单位。所以我们需要考虑到这一点。

算术是一种方法:

where mod(timestamp, 24*60*60*1000) >= 7 * 60*60*1000 and
      mod(timestamp, 24*60*60*1000) < 12 * 60*60*1000

或转换为时间:

where time(from_unixtimestamp(timestamp / 1000)) >= '07:00:00' and
      time(from_unixtimestamp(timestamp / 1000)) < '12:00:00'

事实证明,这些是微妙的不同。算术方法在 UTC 中运行。该from_unixtimestamp()方法似乎考虑了日列表节省时间。


推荐阅读