现在的情况是这样 ,根据当前时间录入用户输入的关键字。
现在有个需求需要把所有的数据按照每天24小时,分成24个段,查询每段时间用户输入关键字
这个要怎么做?求高手指点 。高分相送
CREATE TABLE IF NOT EXISTS `ecnavi_keywords` (
`id` int(10) unsigned zerofill NOT NULL AUTO_INCREMENT,
`magazine_id` varchar(50) DEFAULT NULL,
`page_number` varchar(50) DEFAULT NULL,
`goods_id` mediumint(30) DEFAULT NULL,
`addtime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;
INSERT INTO `ecnavi_keywords` VALUES (0000000006, '6', '096', 6, '2011-04-25 19:19:27');
INSERT INTO `ecnavi_keywords` VALUES (0000000007, '6', '096', 6, '2011-04-25 19:19:29');
INSERT INTO `ecnavi_keywords` VALUES (0000000008, '6', '096', 6, '2011-04-25 19:19:32');
INSERT INTO `ecnavi_keywords` VALUES (0000000009, '6', '096', 23, '2011-04-25 19:19:38');
INSERT INTO `ecnavi_keywords` VALUES (0000000010, '6', '096', 6, '2011-04-25 19:19:41');
INSERT INTO `ecnavi_keywords` VALUES (0000000011, '6', '096', 6, '2011-04-25 19:19:53');
现在有个需求需要把所有的数据按照每天24小时,分成24个段,查询每段时间用户输入关键字
这个要怎么做?求高手指点 。高分相送
CREATE TABLE IF NOT EXISTS `ecnavi_keywords` (
`id` int(10) unsigned zerofill NOT NULL AUTO_INCREMENT,
`magazine_id` varchar(50) DEFAULT NULL,
`page_number` varchar(50) DEFAULT NULL,
`goods_id` mediumint(30) DEFAULT NULL,
`addtime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;
INSERT INTO `ecnavi_keywords` VALUES (0000000006, '6', '096', 6, '2011-04-25 19:19:27');
INSERT INTO `ecnavi_keywords` VALUES (0000000007, '6', '096', 6, '2011-04-25 19:19:29');
INSERT INTO `ecnavi_keywords` VALUES (0000000008, '6', '096', 6, '2011-04-25 19:19:32');
INSERT INTO `ecnavi_keywords` VALUES (0000000009, '6', '096', 23, '2011-04-25 19:19:38');
INSERT INTO `ecnavi_keywords` VALUES (0000000010, '6', '096', 6, '2011-04-25 19:19:41');
INSERT INTO `ecnavi_keywords` VALUES (0000000011, '6', '096', 6, '2011-04-25 19:19:53');
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
001 我的心态乱 2011-04-25 18:30:25
001 我的心态乱 2011-04-25 18:59:25
002 我的心态痛 2011-05-25 19:30:25
003 我的心态痛 2011-06-25 20:30:25按照小时排序,18-19为一个小时 ,19-20为另一个时间段,每个时间段间隔1个小时。
排序后的数据为
排名 时间 keywords次数
1 第18小时 2
2 第19小时 1
3 第20小时 1
类似这样的结果
INSERT INTO `ecnavi_keywords` VALUES (0000000006, '6', '096', 6, '2011-04-26 19:19:27');
INSERT INTO `ecnavi_keywords` VALUES (0000000007, '6', '096', 6, '2011-04-27 19:19:29');
INSERT INTO `ecnavi_keywords` VALUES (0000000008, '6', '096', 6, '2011-04-28 20:19:32');
INSERT INTO `ecnavi_keywords` VALUES (0000000009, '6', '096', 23, '2011-04-29 21:19:38');
INSERT INTO `ecnavi_keywords` VALUES (0000000010, '6', '096', 6, '2011-05-01 22:19:41');
INSERT INTO `ecnavi_keywords` VALUES (0000000011, '6', '096', 6, '2011-05-02 24:19:53');
刚才那个数据的时间跨度不太,例如这样的数据
我希望的结果是
1 第1小时 0
1 第2小时 0
1 第3小时 0
1 第4小时 0
1 第5小时 0
1 第6小时 0
1 第7小时 0
1 第8小时 0
1 第9小时 0
1 第10小时 0
1 第11小时 0
1 第12小时 0
1 第13小时 0
1 第14小时 0
1 第15小时 0
1 第16小时 0
1 第17小时 0
1 第18小时 0
1 第19小时 2
1 第20小时 1
1 第21小时 0
1 第22小时 1
1 第23小时 0
1 第24小时 1
+------------+-------------+-------------+----------+---------------------+
| id | magazine_id | page_number | goods_id | addtime |
+------------+-------------+-------------+----------+---------------------+
| 0000000006 | 6 | 096 | 6 | 2011-04-26 19:19:27 |
| 0000000007 | 6 | 096 | 6 | 2011-04-27 19:19:29 |
| 0000000008 | 6 | 096 | 6 | 2011-04-28 20:19:32 |
| 0000000009 | 6 | 096 | 23 | 2011-04-29 21:19:38 |
| 0000000010 | 6 | 096 | 6 | 2011-05-01 22:19:41 |
| 0000000011 | 6 | 096 | 6 | 0000-00-00 00:00:00 |
+------------+-------------+-------------+----------+---------------------+
6 rows in set (0.03 sec)mysql> create table t(id int primary key);
Query OK, 0 rows affected (0.03 sec)mysql> insert into t values
-> (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(1
7),(18),(19),(20),(21),(22),(23),(24);
Query OK, 24 rows affected (0.02 sec)
Records: 24 Duplicates: 0 Warnings: 0mysql> select * from t;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 20 |
| 21 |
| 22 |
| 23 |
| 24 |
+----+
24 rows in set (0.00 sec)mysql> select 1,concat('第',t.id,'小时'),count(ecnavi_keywords.id)
-> from t left join ecnavi_keywords on t.id=if(hour(addtime)=0,24,hour(addtime))
-> group by t.id;
+---+--------------------------+---------------------------+
| 1 | concat('第',t.id,'小时') | count(ecnavi_keywords.id) |
+---+--------------------------+---------------------------+
| 1 | 第1小时 | 0 |
| 1 | 第2小时 | 0 |
| 1 | 第3小时 | 0 |
| 1 | 第4小时 | 0 |
| 1 | 第5小时 | 0 |
| 1 | 第6小时 | 0 |
| 1 | 第7小时 | 0 |
| 1 | 第8小时 | 0 |
| 1 | 第9小时 | 0 |
| 1 | 第10小时 | 0 |
| 1 | 第11小时 | 0 |
| 1 | 第12小时 | 0 |
| 1 | 第13小时 | 0 |
| 1 | 第14小时 | 0 |
| 1 | 第15小时 | 0 |
| 1 | 第16小时 | 0 |
| 1 | 第17小时 | 0 |
| 1 | 第18小时 | 0 |
| 1 | 第19小时 | 2 |
| 1 | 第20小时 | 1 |
| 1 | 第21小时 | 1 |
| 1 | 第22小时 | 1 |
| 1 | 第23小时 | 0 |
| 1 | 第24小时 | 1 |
+---+--------------------------+---------------------------+
24 rows in set (0.00 sec)mysql>