- 根据 UNIX_TIMESTAMP 去掉分钟后的的位数
思路如下select UNIX_TIMESTAMP(now()) '当前时间 秒',now() '当前时间',FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(CURRENT_TIMESTAMP) / (3 * 60)) * (3 * 60)) '3分钟为分隔去掉多余位数'当前时间 秒 当前时间 3分钟为分隔去掉多余位数
1716198937 2024-05-20 17:55:37 2024-05-20 17:54:00
- 测试数据如下
CREATE TABLE `test` (`dt` datetime DEFAULT NULL,`t` int(255) DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;-- 准备测试数据
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:00:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:1:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:2:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:3:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:4:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:5:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:6:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:7:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:8:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:9:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:10:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:11:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:12:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:13:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:14:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:15:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:16:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:17:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:18:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:19:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:20:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:21:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:22:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:23:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:24:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:25:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:26:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:27:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:28:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:29:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:30:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:31:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:32:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:33:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:34:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:35:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:36:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:37:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:38:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:39:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:40:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:41:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:42:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:43:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:44:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:45:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:46:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:47:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:48:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:49:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:50:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:51:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:52:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:53:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:54:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:55:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:56:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:57:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:58:21', 1);
INSERT INTO .`test`(`dt`, `t`) VALUES ('2024-05-20 17:59:21', 1);-- 分隔语句
select UNIX_TIMESTAMP(dt), FLOOR(UNIX_TIMESTAMP(dt) / (3 * 60)) * (3 * 60),
dt,
FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(dt) / (3 * 60)) * (3 * 60))
from test
order by dt asc -- 统计
select count(1) c,
FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(dt) / (3 * 60)) * (3 * 60))
from test
group by FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(dt) / (3 * 60)) * (3 * 60))
时间按三分钟切块,后进行统计