我想将特定间隔的日期分为10部分,并在mysql中查询每个部分的计数

发布于 2024-12-09 06:39:11 字数 181 浏览 0 评论 0原文

我有一张桌子。

它有两个字段iddatetime

我需要做的是,对于任意两个给定的日期时间,我需要将时间范围分为 10 个相等的间隔,并给出每个间隔的行数。

请让我知道这是否可以在不使用 java 或 php 等语言的任何外部支持的情况下实现。

I have a table.

And it has two fields id and datetime.

What I need to do is, for any two given datetimes, I need to divide the time range into 10 equal intervals and give row count of each interval.

Please let me know whether this is possible without using any external support from languages like java or php.

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

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

发布评论

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

评论(2

手心的温暖 2024-12-16 06:39:11
select ((UNIX_TIMESTAMP(date_col) / CAST((time2 - time1)/10) AS INT) + time1), count(id) from my_table where date_col >= time1 AND date_col <= time2 GROUP BY ((UNIX_TIMESTAMP(date_col) / CAST((time2 - time1)/10) AS INT) + time1)

我还没有测试过。但这样的事情应该有效。

select ((UNIX_TIMESTAMP(date_col) / CAST((time2 - time1)/10) AS INT) + time1), count(id) from my_table where date_col >= time1 AND date_col <= time2 GROUP BY ((UNIX_TIMESTAMP(date_col) / CAST((time2 - time1)/10) AS INT) + time1)

I haven't tested it. But something like this should work.

谜兔 2024-12-16 06:39:11

划分日期间隔的最简单方法是将它们存储为长整型(即从“时间开始”开始的 #of 刻度)。据我所知,没有办法使用MySQL的日期时间来做到这一点。

您决定如何做最终取决于您的应用程序。我会将它们存储为长整型,并让您使用的任何前端处理转换为更易读的格式。

给出每个间隔的行数到底是什么意思?这部分对我来说没有意义。

The easiest way to divide date intervals is if you store them as longs (ie #of ticks from the "beginning of time"). As far as I know, there is no way to do it using MySQL's datetime.

How you decide to do it ultimately depends on your application. I would store them as longs and have whatever front end you are using handle to conversion to a more readable format.

What exactly do you mean by giving the row count of each interval? That part doesn't make sense to me.

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