MySQL Group By custom timestamp

亡梦爱人 提交于 2019-12-23 23:44:07

问题


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 ?


回答1:


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))


来源:https://stackoverflow.com/questions/6884207/mysql-group-by-custom-timestamp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!