2013-04-09 125 views
0

我有一個包含文章記錄的表。現在我想計算源文章之間的平均時間,所以我可以自動增加或減少我的抓取時間間隔。如果我在兩個日期之間從一個源中選擇記錄,那麼用MySQL Query可以嗎?記錄之間的平均時間MySQL

表佈局:

id | title | description | create_date   | source_id 
----------------------------------------------------------------- 
1 | Title 1 | A description | 2013-04-09 21:38:00 | 5 
2 | Title 2 | A description | 2013-04-09 22:16:00 | 5 
3 | Title 3 | A description | 2013-04-09 23:54:00 | 5 

結果應該是:

diff between 1 and 3 = 136 min 
----------------------------- 
Average is 45,33 minutes (crawler interval: 2720 seconds) 

我也能做到這一點在PHP中,如果這是更好!

+0

試試這個: http://stackoverflow.com/questions/2237159/get-average-time-between-times-in-sql 或本: http://stackoverflow.com/questions/2237159/get-average-time-between-times-in-sql 乾杯:) – mCube 2013-04-09 21:10:32

回答

2

是的。

SELECT 
    UNIX_TIMESTAMP(MAX(create_time)) - UNIX_TIMESTAMP(MIN(create_time))/COUNT(*) AS 'avg' 
FROM table 
WHERE source_id = 5 
    AND create_time BETWEEN '2013-04-01 00:00:00' AND NOW()