MySql查询按时间分组 [英] MySql Query Grouping by Time

查看:111
本文介绍了MySql查询按时间分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一份报告来了解订单被放置的时间,因此我需要按时间对它们进行总结和分组。例如,我想要在1到1:59之间放置所有订单的总和,然后下一行列出2:00到2:59之间所有订单的总和等。该字段是日期时间变量,但对于生活我我一直无法找到正确的查询来做到这一点。任何建议将我送到正确的道路将不胜感激。

I am trying to create a report to understand the time-of-day that orders are being placed, so I need to sum and group them by time. For example, I would like a sum of all orders placed between 1 and 1:59, then the next row listing the sum of all orders between 2:00 and 2:59, etc. The field is a datetime variable, but for the life me I haven't been able to find the right query to do this. Any suggestions sending me down the right path would be greatly appreciated.

谢谢

Thanks

推荐答案

如果运气好的话,它是mysql,通过订单总数来表示订单数量而不是数量:

If by luck it is mysql and by sum of orders you mean the number of orders and not the value amount:

select date_format(date_field, '%Y-%m-%d %H') as the_hour, count(*)
from my_table
group by the_hour
order by the_hour

这篇关于MySql查询按时间分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆