我在mysql中有一个如下所示的表:
ID timestamp
----------------------------
F41 2016-08-27 00:25:58
F42 2016-08-27 01:15:03
F43 2016-08-27 02:14:30
F44 2016-08-27 02:24:12
F45 2016-08-27 03:05:46
F46 2016-08-27 04:08:07
F47 2016-08-27 05:10:35
F48 2016-08-27 07:12:32
F59 2016-08-27 08:21:19
F50 2016-08-27 09:19:26
F51 2016-08-27 11:01:56
F52 2016-08-27 12:09:38
F53 2016-08-27 13:42:48
F54 2016-08-27 14:47:26
F55 2016-08-27 15:24:12
F56 2016-08-27 16:22:05
F57 2016-08-27 18:20:47
F58 2016-08-27 19:11:09
F59 2016-08-27 19:41:47
F60 2016-08-27 19:57:06
F61 2016-08-27 20:12:45
F62 2016-08-27 21:55:41
F63 2016-08-27 22:17:38
F64 2016-08-27 23:15:17
ID列不是自动增量。
我需要创建一个视图来计算每小时的记录数,如下所示:
ID time count
------------------------------------------
1 00:00:00 1
2 01:00:00 1
3 02:00:00 1
4 03:00:00 2
5 04:00:00 1
6 05:00:00 1
7 06:00:00 1
8 07:00:00 0
9 08:00:00 1
10 09:00:00 1
11 10:00:00 1
12 11:00:00 0
13 12:00:00 1
14 13:00:00 1
15 14:00:00 1
16 15:00:00 1
17 16:00:00 1
18 17:00:00 1
19 18:00:00 0
20 19:00:00 1
21 20:00:00 3
22 21:00:00 1
23 22:00:00 1
24 23:00:00 1
我尝试使用UNION ALL并且它有效,但我不知道如何加入ID列和时间列。
答案 0 :(得分:2)
你可以这样做:
SELECT @rownum := @rownum + 1 AS ID,
CONCAT(HOUR(`timestamp`), ':00:00') AS `time`,
COUNT(*) AS `count`
FROM table, (SELECT @rownum := 0) r
GROUP BY HOUR(`timestamp`)
答案 1 :(得分:1)
我会说你应该按小时分组,而不是使用工会。
SELECT HOUR(timestamp) time, COUNT(*) count FROM table GROUP BY time.
这种方式没有id,时间会有0到23之间的数字,而count会有从那个小时开始的记录数。
答案 2 :(得分:1)
请注意每小时""在20:00:00之前,所以20:00:00的计数是3(F58,F59,F60),19:00:00的计数是1(F57);
一些额外的工作:P
select @rownum := @rownum + 1 AS ID,
concat((case when t.hour = 24 then 0 else t.hour end),':00:00') as time,
count(*) as count
from (select id, hour(date_format(time,'%H:%i:%s'))+1 as hour from st) t,
(SELECT @rownum := 0) r
group by time
order by ID;
用于创建测试表和测试数据的SQL:
CREATE TABLE `st` (
`id` varchar(8) NOT NULL,
`time` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
insert into st values('F41','2016-08-27 00:25:58');
insert into st values('F42','2016-08-27 01:15:03');
insert into st values('F43','2016-08-27 02:14:30');
insert into st values('F44','2016-08-27 02:24:12');
insert into st values('F45','2016-08-27 03:05:46');
insert into st values('F46','2016-08-27 04:08:07');
insert into st values('F47','2016-08-27 05:10:35');
insert into st values('F48','2016-08-27 07:12:32');
insert into st values('F59','2016-08-27 08:21:19');
insert into st values('F50','2016-08-27 09:19:26');
insert into st values('F51','2016-08-27 11:01:56');
insert into st values('F52','2016-08-27 12:09:38');
insert into st values('F53','2016-08-27 13:42:48');
insert into st values('F54','2016-08-27 14:47:26');
insert into st values('F55','2016-08-27 15:24:12');
insert into st values('F56','2016-08-27 16:22:05');
insert into st values('F57','2016-08-27 18:20:47');
insert into st values('F58','2016-08-27 19:11:09');
insert into st values('F59','2016-08-27 19:41:47');
insert into st values('F60','2016-08-27 19:57:06');
insert into st values('F61','2016-08-27 20:12:45');
insert into st values('F62','2016-08-27 21:55:41');
insert into st values('F63','2016-08-27 22:17:38');
insert into st values('F64','2016-08-27 23:15:17');
学习构建auto_increament专栏的更好方法,thx @Jacobian
答案 3 :(得分:1)
查询:
SELECT HOUR(timestamp), COUNT(*) count FROM my_table GROUP BY HOUR(timestamp);
此问题的其余部分是显示问题,最好在应用程序级代码中解决