Tstamp间隔始终在午夜开始

时间:2018-10-16 16:47:06

标签: mysql timestamp intervals

我有这个查询,向我显示了1小时的间隔。我的问题是,将其设置为1小时时,它可以显示从12AM到11PM的完美数据。

SELECT 

FROM_UNIXTIME(UNIX_TIMESTAMP(t_stamp) - MOD(UNIX_TIMESTAMP(t_stamp), 3600)) as Tstamp,
Col1,
Col2

FROM table


WHERE DATE(t_stamp) BETWEEN '2018-10-15' AND '2018-10-15'


GROUP BY Tstamp

ORDER BY t_stamp ASC

此输出看起来像这样

Tstamp                  Col1    Col2
2018-10-15 00:00:00     73.43   72.12
2018-10-15 01:00:00     73.29   71.96
2018-10-15 02:00:00     73.43   72.21
.
.
.
.
.
2018-10-15 16:00:00     74.24   72.85
2018-10-15 17:00:00     74.37   72.85
2018-10-15 18:00:00     74.20   72.64
2018-10-15 19:00:00     74.01   72.42
2018-10-15 20:00:00     73.80   72.42
2018-10-15 21:00:00     73.73   72.34
2018-10-15 22:00:00     73.59   72.13
2018-10-15 23:00:00     73.51   72.12

但是当我将间隔设置为3小时或8小时时,它不是从午夜开始,例如,如果所选日期是2018-10-15,并且间隔设置为3小时,则它是从2018-10开始-14 11:00 PM而不是2018-10-15 12:00 AM

SELECT 

FROM_UNIXTIME(UNIX_TIMESTAMP(t_stamp) - MOD(UNIX_TIMESTAMP(t_stamp), 10800)) as Tstamp,
Col1,
Col2

FROM table


WHERE DATE(t_stamp) BETWEEN '2018-10-15' AND '2018-10-15'


GROUP BY Tstamp

ORDER BY t_stamp ASC

这是间隔为3小时时的输出

Tstamp                  Col1    Col2
2018-10-14 23:00:00     73.43   72.12
2018-10-15 02:00:00     73.43   72.21
2018-10-15 05:00:00     73.73   72.42
2018-10-15 08:00:00     73.29   71.96
2018-10-15 11:00:00     73.36   72.34
2018-10-15 14:00:00     73.98   72.64
2018-10-15 17:00:00     74.37   72.85
2018-10-15 20:00:00     73.80   72.42
2018-10-15 23:00:00     73.51   72.12

我希望在间隔为3小时8小时的情况下获得这样的输出,如果选择的日期为2018-10-15,它将开始于午夜12点而不是前一天的时间。

Tstamp                  Col1    Col2
2018-10-15 00:00:00     73.43   72.21
2018-10-15 03:00:00     73.72   72.99
2018-10-15 06:00:00     73.34   72.12
2018-10-15 09:00:00     73.43   72.77
2018-10-15 12:00:00     73.29   72.42
2018-10-15 15:00:00     74.15   72.76
2018-10-15 18:00:00     74.20   72.64
2018-10-15 21:00:00     73.73   72.34
.
.
.

非常感谢您的帮助。谢谢

结构和样本数据

CREATE TABLE `table` (
  `Col1` float DEFAULT NULL,
  `Col2` float DEFAULT NULL,
  `t_stamp` datetime DEFAULT NULL,
  KEY `tablet_stampndx` (`t_stamp`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `table`
(`Col1`,
`Col2`,
`t_stamp`)
VALUES
('73.43','72.12','2018-10-15 00:00:00'),
('73.29','71.96','2018-10-15 01:00:00'),
('73.43','72.21','2018-10-15 02:00:00'),
('73.72','72.99','2018-10-15 03:00:00'),
('73.80','72.55','2018-10-15 04:00:00'),
('73.73','72.42','2018-10-15 05:00:00'),
('73.34','72.12','2018-10-15 06:00:00'),
('73.94','73.20','2018-10-15 07:00:00'),
('73.29','71.96','2018-10-15 08:00:00'),
('73.43','72.77','2018-10-15 09:00:00'),
('73.43','72.12','2018-10-15 10:00:00'),
('73.36','72.34','2018-10-15 11:00:00'),
('73.29','72.42','2018-10-15 12:00:00'),
('73.51','72.34','2018-10-15 13:00:00'),
('73.98','72.64','2018-10-15 14:00:00'),
('74.15','72.76','2018-10-15 15:00:00'),
('74.24','72.85','2018-10-15 16:00:00'),
('74.37','72.85','2018-10-15 17:00:00'),
('74.20','72.64','2018-10-15 18:00:00'),
('74.01','72.42','2018-10-15 19:00:00'),
('73.80','72.42','2018-10-15 20:00:00'),
('73.73','72.34','2018-10-15 21:00:00'),
('73.59','72.13','2018-10-15 22:00:00'),
('73.51','72.12','2018-10-15 23:00:00')

到目前为止,我已经尝试使用DATE_SUB函数,但它并没有真正提供所需的输出。我在日期间隔上选择了10-15,但是它向我显示了10-14的数据,这确实不是我想要查看的内容

SELECT 

t_stamp,
Col1,
Col2


FROM table

WHERE t_stamp BETWEEN DATE_SUB('2018-10-15 00:00:00', INTERVAL 3 HOUR) 
    AND DATE_ADD('2018-10-15 23:59:59', INTERVAL 3 HOUR)

GROUP BY t_stamp
ORDER BY t_stamp ASC


2018-10-14 21:02:17 73.50   72.21
2018-10-14 21:07:17 73.51   72.21
2018-10-14 21:12:17 73.50   72.21
2018-10-14 21:17:17 73.50   72.19
2018-10-14 21:22:17 73.50   72.21
2018-10-14 21:27:17 73.50   72.21

1 个答案:

答案 0 :(得分:1)

这是你的追求吗?

SELECT DISTINCT SEC_TO_TIME((FLOOR(TIME_TO_SEC(t_stamp)/10800)*10800))x FROM `table`;