1、建立测试数据表,并填上数据
sql语句:
CREATE TABLE `t` (
`id` int(11) default NULL,
`device_id` int(11) default NULL,
`w` int(11) default NULL,
`create_time` datetime default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2、填充数据,并且用红色笔标出稍后汇总的结果是1,4,2
3、sql查询语句
SELECT FROM_UNIXTIME(t.unit_time * 900, '%Y-%m-%d %H:%i:%s') time, t.sum
FROM
(
SELECT CEIL(UNIX_TIMESTAMP(t.create_time) / 900) as unit_time, sum(t.w) sum FROM t
GROUP BY unit_time
) t;
查询结果: