按 X 天范围分组
我有一组或多条记录,我想对它们进行计数并按一定范围进行分组,例如我想对 X 天组创建的记录进行计数
e.g. SELECT COUNT(*) FROM `table` GROUP BY /*`created` 3 days/*
I have a set or records and I want to count and group them by a certain range e.g. I want to count the records that were created by groups of X days
e.g. SELECT COUNT(*) FROM `table` GROUP BY /*`created` 3 days/*
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个带有日期的示例。
Here is an example with dates.
你可以做类似的事情
SELECT COUNT(*) FROM table GROUP BY FLOOR(created / 3)
...我想。
尽管如果
created
是日期字段,则您必须进行更多调整才能将其转换为数字值才能正常工作。You can do something like
SELECT COUNT(*) FROM table GROUP BY FLOOR(created / 3)
... I think.
Although if
created
is a date field, you'll have to do a little more jiggering to get it into a number value for this to work.谢谢@Ronnis,我使用你的例子并最终解决了我的问题。
我发现了一个小错误,例如,我添加了一行
现在我得到:
如您所见,天分为 2, 3, 3, 3, 2,这不是我所期望的。
我将 SQL 更改为
Just get datediff 从创建到开始日期,现在我得到:
这可能更合适。
Thanks @Ronnis, I use your example and finally solve my problem.
And there is a small mistake I found, in example, I add one row
Now I get:
As you can see that days divide into 2, 3, 3, 3, 2 which not what I expected.
I change SQL as
Just get datediff from created to start date, and now I get:
That might be more suitable.