3
CREATE TABLE `connectionLog` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `serverTimeConnected` datetime NOT NULL,
  `serverTimeDisconnected` datetime  NOT NULL,
  PRIMARY KEY (`ID`)
);

通过这个查询,我得到了 serverTimeConnected 在一小时内的所有 ID。

SELECT  HOUR( `serverTimeConnected`) as STUNDE, 
    count(`ID`) as HITS 
FROM    `connectionLog` 
GROUP BY HOUR( `serverTimeConnected`)

但我需要 serverTimeConnected 和 serverTimeDisconnected 之间的所有 ID。

ID  serverTimeConnected serverTimeDisconnected
1   10:00:00            10:10:00    
2   10:00:00            11:10:00    
3   10:00:00            12:10:00

我需要一个结果——比如

STUNDE  HITS
10,     3
11,     2
12,     1
4

3 回答 3

1

更新:我找到了这个解决方案

 SELECT Stunden.ID, COUNT(Stunden.ID) as HITS FROM Stunden
INNER JOIN connectionLog cl 
  ON (
    (ADDTIME(DATE_FORMAT(cl.serverTimeConnected,'%Y-%m-%d 00:00:00'),CONCAT(Stunden.ID,":00:00")) 
      BETWEEN cl.serverTimeConnected AND cl.serverTimeDisconnected)
    OR
      (ADDTIME(DATE_FORMAT(cl.serverTimedisConnected,'%Y-%m-%d 00:00:00'),CONCAT(Stunden.ID,":00:00")) 
      BETWEEN cl.serverTimeConnected AND cl.serverTimeDisconnected)
     )

GROUP BY Stunden.ID

http://sqlfiddle.com/#!2/6f3b0/11(更新)

该表Stunden是所有可用时间的列表。一个简单INNER JOIN的小时在连接和断开之间(看看一天)让我们得到我们搜索的匹配数。然后Group by小时和我们匹配吧HITS

于 2013-10-25T14:53:05.313 回答
0

怎么样group_concat

SELECT  HOUR( `serverTimeConnected`) as STUNDE, 
        count(`ID`) as HITS,
        group_concat(id) as IDs
FROM    `connectionLog` 
GROUP BY HOUR( `serverTimeConnected`)
于 2013-10-25T12:40:02.493 回答
-1

你可以看看TIMEDIFF

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_timediff

于 2013-10-28T14:38:28.007 回答