将日期时间字段分组为块

发布于 2024-10-16 00:19:42 字数 154 浏览 4 评论 0原文

我有一个带有字段 datestamp (datetime) 的 mysql MyIsam 表,我试图返回过去 3 小时内具有 datestamp 的所有记录,但将其分为 10 个块(因此对于本示例,第一个组将是前 18 分钟)等等...)

有什么想法吗?这在mysql中可能吗?

I have a mysql MyIsam table with a field datestamp (datetime), I am trying to return all records that have a datestamp within the last 3 hours, but group it into 10 chunks (so for this example group one would be the first 18 minutes etc...)

Any ideas? Is this possible in mysql?

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

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

发布评论

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

评论(3

她说她爱他 2024-10-23 00:19:42

我认为这可能有效:

$t = date("U",strtotime("-3 Hours"));
$sql = "SELECT *, FLOOR((UNIX_TIMESTAMP(datestamp) - ".$t.") / 1080) as chunk 
    FROM table WHERE UNIX_TIMESTAMP(datestamp) > '".$t."' GROUP BY chunk";

将差异除以 3 小时间隔(1080 秒)的 1/10。这给出了块 0,1,2...9,其中 0 表示前 18 分钟。

I think this may work:

$t = date("U",strtotime("-3 Hours"));
$sql = "SELECT *, FLOOR((UNIX_TIMESTAMP(datestamp) - ".$t.") / 1080) as chunk 
    FROM table WHERE UNIX_TIMESTAMP(datestamp) > '".$t."' GROUP BY chunk";

Devide the difference with 1/10th of the 3 hour interval (1080 sec). This gives chunks 0,1,2...9, where 0 means the first 18 minutes.

深巷少女 2024-10-23 00:19:42

当然。因为我并不是真正的 MySQL 专家,所以这是伪代码:

SELECT ..., (now() - datestamp) AS age, age / 600 AS group, ...

然后你有两列,“年龄”用于限制选择,“组”进一步细分。

Sure. As I'm not really a MySQL expert, this is pseudocode:

SELECT ..., (now() - datestamp) AS age, age / 600 AS group, ...

Then you have two columns, "age" that you use to limit the selection, and "group" that further subdivides.

浊酒尽余欢 2024-10-23 00:19:42

像10800这样使用

   SELECT SUM(myfields)..... FROM mytable  
    WHERE mydate >= DATE_SUB( NOW() , INTERVAL 3 HOUR) 
    GROUP BY FLOOR((UNIX_TIMESTAMP() - UNIX_TIMESTAMP(mydate) ) / 10800)

是3小时内的秒数

use like

   SELECT SUM(myfields)..... FROM mytable  
    WHERE mydate >= DATE_SUB( NOW() , INTERVAL 3 HOUR) 
    GROUP BY FLOOR((UNIX_TIMESTAMP() - UNIX_TIMESTAMP(mydate) ) / 10800)

10800 is the number of second in 3 hours

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