0
ID         Date
1          2013-01-11 00:57:25 
2          2013-02-01 02:17:05 
3          2013-02-05 03:12:23
4          2013-02-17 10:17:02
5          2013-02-19 11:27:25
6          2013-02-27 13:42:13
7          2013-03-09 15:57:25
8          2013-03-12 16:00:00
9          2013-04-10 19:39:18
10         2013-05-16 20:46:38

我想Group ID根据这张表中的时间。请记住,日期在一列中,所以我可以只从日期列中获取时间并根据不同的时间对其进行分组。

就像我想ID00:00:00 to 12:00:00一个组12:00:00 to 24:00:00中的时间和另一个组中的mysql.

4

2 回答 2

3
SELECT
CASE WHEN TIME(`Date`) BETWEEN '00:00:00' AND '12:00:00' THEN 'early' ELSE 'late' END AS myTime,
COUNT(*)
FROM yourTable
GROUP BY myTime

您还可以轻松添加多个“组”:

SELECT
CASE WHEN TIME(`Date`) BETWEEN '00:00:00' AND '03:00:00' THEN '0-3' 
CASE WHEN TIME(`Date`) BETWEEN '03:00:01' AND '06:00:00' THEN '3-6'
ELSE 'later' END AS myTime,
COUNT(*)
FROM yourTable
GROUP BY myTime
于 2013-05-28T09:03:03.087 回答
1

暗示:

SELECT HOUR(Date), COUNT(*)
FROM your_table
GROUP BY HOUR(Date);
于 2013-05-28T09:02:49.100 回答