mysql中如何使用group by关键字获取每天的信息
我是 mysql 新手,目前遇到查询问题。我需要获得一周内每天每项活动的平均持续时间。日期格式如下:'2000-01-01 01:01:01',但我想摆脱 01:01:01 的东西,只关心日期。我该怎么做?
表格是这样的:
record_id int(10) NOT NULL,
activity_id varchar(100) NOT NULL,
start_time datetime NOT NUll,
end_time datetime NOT NULL,
duration int(10) NOT NULL;
谢谢。
I'm new in mysql and I'm currently having an issue with a query. I need to get an average duration for each activity each day within a week. The date format is like: '2000-01-01 01:01:01', but I want to get rid of the 01:01:01 thing and only care about the date. How do I do that?
The table is something like this:
record_id int(10) NOT NULL,
activity_id varchar(100) NOT NULL,
start_time datetime NOT NUll,
end_time datetime NOT NULL,
duration int(10) NOT NULL;
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以做类似下面的事情
You could do something like the following
如果我理解的话,您希望按不同的活动时间进行分组,并查看每项活动开始和结束之间的平均天数。这应该适合你。
编辑:误解了,您还希望它按天细分,因此这应该拉动每个组,按 start_time 所在的星期几细分,然后是 start_time 和 end_time 之间的平均天数。
If I'm understanding, you want to group by the different activity times and see the average days between the start and end of each activity. This should do it for you.
Edit: Misunderstood, you want it broken down by the day as well, so this should pull each group, broken down by the day of the week that the start_time falls on, and then the average days between start_time and end_time.
您可以使用 date_format(end_time, '%Y%m%d') 将其转换为可按天排序的值。将其按表达式放入组中,这应该可以满足您的要求。
You can use date_format(end_time, '%Y%m%d') to convert it to a sortable value by day. Put that in the group by expression, and that should do what you want.