2013-05-28 74 views
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根據時間在這張表中。請記住日期在一列中,所以我可以根據不同的時間從日期列和組中只取時間。按時間分組,其中時間是在MySQL日期列?

像我想ID從一組00:00:00 to 12:00:0012:00:00 to 24:00:00在另一組在mysql

回答

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 
1

提示:

SELECT HOUR(Date), COUNT(*) 
FROM your_table 
GROUP BY HOUR(Date);