mysql - 当数据为纪元时间格式时,如何使用 sql 查询获取特定时间范围?
问题描述
我想获取时间范围在上午 7 点到 12 点之间的数据,但数据是纪元时间格式。请帮我解决一下这个。
数据库如下所示:
ID Timestamp
1 1591622630224
2 1591622632812
3 1591622635282
解决方案
嗯。. . 您的 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()
方法似乎考虑了日列表节省时间。
推荐阅读
- javascript - React - 多张图片上传时间错误(Firebase Firestore 和 Storage)
- apache - 不可见的htaccess重定向保持可见问题
- java - Hibernmate + Postgres dosent create tables on first run
- http - http get on dart 需要很长时间才能返回响应状态
- visual-studio - 将 Visual Studo 连接到源代码管理时 URI 无效
- python - 拆分一列并在括号之间获取字符串
- reactjs - 从数组字段中的文档 id 获取 firestore 文档并在 React 中显示
- java - AspectJ 与 Spring AOP 相结合
- c++ - 将元素插入数组时面临问题
- python - 您如何获取键的值并将其添加到列表中?