我有一个包含文章记录的表格。现在我想计算源文章之间的平均时间,因此我可以自动增加或减少我的爬虫间隔。如果我在两个日期之间选择来自一个来源的记录,这可能是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 :(得分: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()