Mysql DateTime 组按 15 分钟

Mysql DateTime group by 15 mins(Mysql DateTime 组按 15 分钟)

本文介绍了Mysql DateTime 组按 15 分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张像这样的桌子

CREATE TABLE `time_table` (
 `id` INT(10) NOT NULL AUTO_INCREMENT,
 `creationDate` DATETIME NOT NULL,
 PRIMARY KEY (`id`)
)

我基本上将某些记录的创建时间存储在表中.我知道如果我想获得以 15 分钟为间隔创建的记录的计数,我将使用类似这样的方法

I basically store the creation time of certain records in the table. I know if I want to get a count of the records that were created in 15 mins interval I will use something like this

SELECT FLOOR(UNIX_TIMESTAMP(creationDate)/900) AS t, 
COUNT(*) FROM time_table
GROUP BY t

这给了我这样的东西

t          COUNT(*)
1434187    1
1434188    3
1434189    2
1434190    2

我如何理解第一列?如果我想让它向我展示类似

How do I make sense of the first column? If I want it to show me something like

t                 COUNT(*)
2:00pm - 2:15pm   1
2:15pm - 2:30pm   3
2:30pm - 2:45pm   2
2:45pm - 3:00pm   2

我知道通过一些操作,我可以让 1434187 在下午 2:15 出现.即使那可能是一个好的开始......然后通过一些逻辑我可以展示整个时期.谢谢!

I understand that with some manipulation I could get 1434187 to show up at 2:15pm. Even that might be a good start....then with some logic I could show the entire period. Thanks!

推荐答案

一种方法是只使用 >< 以获得范围内的所有内容,但您可能会发现这更简单:

One way is to just use > and < in order to get everything within a range, but you may find this to be simpler:

SELECT ... 

GROUP BY ( 4 * HOUR( thistime ) + FLOOR( MINUTE( thistime ) / 15 )) 

来自http://forums.mysql.com/read.php?10,202789,202807

这篇关于Mysql DateTime 组按 15 分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Mysql DateTime 组按 15 分钟

基础教程推荐