MySQL Group By 自定义时间戳

发布于 2024-11-27 06:22:32 字数 191 浏览 1 评论 0原文

我想从按日期分组的数据库中获取结果。生成的元组应按日期分组,日期范围为上午 5 点到凌晨 5 点。换句话说,截止时间应该是凌晨 5 点,而不是午夜 12 点。我可以执行 GROUP BY DAY(timestamp) 按正常日期进行分组,但是如果我必须改变时间并且每条记录应从上午 5 点到凌晨 5 点分组怎么办?我应该如何更改查询?谢谢 ?

I want to get results from the database which is grouped by date. The resulting touples should be grouped by date which ranges from 5 am to 5 am. In other words the cutoff should be on 5 am and not midnight 12 am. I can do GROUP BY DAY(timestamp) to group by normal day but what if I have to vary the time and each records should be grouped from 5 am to 5 am ? How should I change the query ? Thanks ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你怎么敢 2024-12-04 06:22:32

只需从每个日期时间值中减去 5 小时,然后将其转换为日期即可。然后您可以根据结果进行分组。

GROUP BY DATE(DATE_SUB(timestamp, INTERVAL 5 HOUR))

Simply subtract 5 hours from each datetime value you have, and then convert it to a date. Then you can group by the result.

GROUP BY DATE(DATE_SUB(timestamp, INTERVAL 5 HOUR))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文