2011-09-14 111 views
0
SELECT 
     COUNT(t1.tid) AS count, 
     u.user_name, 
     SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(t1.endtime,t1.endtime)))/COUNT(t1.endtime)) AS `avg`, 
     MAX(TIMEDIFF(t1.endtime,t1.starttime)) AS `max`, 
     MIN(TIMEDIFF(t1.endtime,t1.starttime)) AS `min`, 
     CONCAT(
      IF(t1.starttime>=(NOW() - INTERVAL 15 MINUTE), '15', ''), 
      IF(t1.starttime>=(NOW() - INTERVAL 30 MINUTE), '30', ''), 
      IF(t1.starttime>=(NOW() - INTERVAL 1 HOUR), 'H', ''), 
      IF(t1.starttime>=(NOW() - INTERVAL 1 DAY), 'D', ''), 
      IF(t1.starttime>=DATE_FORMAT(SUBDATE(NOW(), INTERVAL WEEKDAY(NOW()) DAY), '%Y-%m-%d'), 'W', ''), 
      IF(t1.starttime>=CAST(DATE_FORMAT(NOW() ,'%Y-%m-01') AS DATE), 'M', '') 
     ) AS period, 
     t1.starttime 
    FROM table1 t1 
    LEFT OUTER JOIN `user` u ON u.id = t1.user_id 
    WHERE t1.starttime >= CAST(DATE_FORMAT(NOW() ,'%Y-%m-01') AS DATE) 
    GROUP BY user_name, period 

以上是我曾經做過一個簡單的查詢得到的數據。我的意圖是獲得各種時間表的結果。即獲得數,最小,爲去年15分鐘,1小時,1天,1周,1個月等MySQL查詢到從不同時間段

所以,我要的是結果是這樣的最大值等:

+--------------+--------------+-----------------+--------------+-----------------+ 
| period  | count  | user   | min   | max    | 
+--------------+--------------+-----------------+--------------+-----------------+ 
| 15mins  | 12   | test_user1 | 00:00:45 | 00:01:45  | 
| 15mins  | 12   | test_user2 | 00:00:45 | 00:01:45  | 
| 15mins  | 12   | test_user3 | 00:00:45 | 00:01:45  | 
| 15mins  | 12   | test_user4 | 00:00:45 | 00:01:45  | 
| 15mins  | 12   | test_user5 | 00:00:45 | 00:01:45  | 
| 30mins  | 15   | test_user1 | 00:01:45 | 00:11:45  | 
| 30mins  | 15   | test_user2 | 00:01:45 | 00:11:45  | 
| 30mins  | 16   | test_user3 | 00:04:45 | 00:11:45  | 
| 30mins  | 18   | test_user4 | 00:09:45 | 00:11:45  | 
| 30mins  | 19   | test_user10 | 00:07:45 | 00:11:45  | 
| 30mins  | 14   | test_user11 | 00:04:45 | 00:11:45  | 
| 30mins  | 15   | test_user12 | 00:00:45 | 00:11:45  | 
| 1day  | 100  | test_user1 | 01:00:45 | 01:11:45  | 
+--------------+--------------+-----------------+--------------+-----------------+ 

T1。開始時間是日期時間。我們根據當前的服務器時間獲得結果。

+0

是什麼你現在得到了嗎? –

回答

0

如果間隔嵌套(分15個30分鐘,1小時等),那麼你就不能迴避工會和您的查詢(我簡化了一下)看起來就像這樣:

select count(t1.tid) as count, '15 min' period, userid , 
SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(t1.endtime,t1.endtime)))/COUNT(t1.endtime)) AS `avg`, 
     MAX(TIMEDIFF(t1.endtime,t1.starttime)) AS `max`, 
     MIN(TIMEDIFF(t1.endtime,t1.starttime)) AS `min` 

from t1 
where timestampdiff(minute,t1.starttime,now()) < 15 
group by userid 
UNION 
select count(t1.tid) as count, '30 min' , userid , 
SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(t1.endtime,t1.endtime)))/COUNT(t1.endtime)) AS `avg`, 
     MAX(TIMEDIFF(t1.endtime,t1.starttime)) AS `max`, 
     MIN(TIMEDIFF(t1.endtime,t1.starttime)) AS `min` 

from t1 
where timestampdiff(minute,t1.starttime,now()) < 30 
group by userid 
UNION 
select count(t1.tid) as count, '60 min', userid , 
SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(t1.endtime,t1.endtime)))/COUNT(t1.endtime)) AS `avg`, 
     MAX(TIMEDIFF(t1.endtime,t1.starttime)) AS `max`, 
     MIN(TIMEDIFF(t1.endtime,t1.starttime)) AS `min` 

from t1 
where timestampdiff(minute,t1.starttime,now()) < 60 
group by userid 
/* and so on */ 
order by period, userid 
+0

你確定這不能沒有UNION嗎? 是否可以向GROUP BY添加句點可以產生正確的結果? 目前,PHP編碼執行不同的查詢,並結合結果(這是緩慢的)。所以即時通訊嘗試在沒有聯合的單個查詢中執行所有操作 – Prasanth

+0

實際上,聯合查詢是單個並且在同一個事務中運行。它不是PHP,它是一個純粹的MySQL查詢。無法構建查詢無w/o聯合,因爲您不能從同一列的單個行中生成2行。而且沒有必要按期間分組,因爲每個部分都具有相同的週期值,所以它最初是分組的。 – mishau

+0

我指的是在我的查詢中按句點分組。我知道你的子查詢有相同的時間。 我試過UNION查詢,需要7秒才能完成。幾乎與當前的設置類似。 – Prasanth