Mysql: Calculate averge time between visits

谁说我不能喝 提交于 2020-01-25 04:44:06

问题


This related to my other question.

I have this table

CREATE OR REPLACE TABLE hits (ip bigint, page VARCHAR(256), agent VARCHAR(1000), 
                              date datetime)

and I want to calculate average time between googlebot visit for every page.

... WHERE agent like '%Googlebot%' group by page order by date

Something like

select datediff('2010-09-18 04:20:47', '2010-09-16 05:23:04')

but for every date in table
If there is no mysql way, how can I do this in php?


回答1:


SELECT page, TIMESTAMPDIFF(SECOND, MIN(date), MAX(date)) / (COUNT(*)-1) FROM hits 
WHERE agent like '%Googlebot%' GROUP BY page;

TIMESTAMPDIFF(SECOND, a, b) returns the difference in seconds between the date expressions a and b. For each page, the query finds the date of the first and last visit and the total count of visits, and calculates the arithmetic average.



来源:https://stackoverflow.com/questions/3745101/mysql-calculate-averge-time-between-visits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!