如何使用mysql计算我的网站访问次数?

时间:2022-11-29 21:14:34

I am storing all visits to my site in a table, I store the date, the page visited and a session id.

我将所有访问存储在我的网站的表中,我存储日期,访问的页面和会话ID。

In theory, I can group somebody by their session id and this counts as 1 visit.

从理论上讲,我可以根据他们的会话ID对某人进行分组,这算作一次访问。

What I'd like to do however is go through the table and get the total of visits for each date. So it would group by the session id, and then group by the date.

然而,我想要做的是通过表格获得每个日期的访问总数。因此它将按会话ID分组,然后按日期分组。

ie:

即:

SELECT DATE(added) as date, COUNT(*) FROM visits GROUP BY sessionID, date

This doesn't work as it retrieves then the total of visits for that session id, and the date.

这不起作用,因为它检索该会话ID的访问总数和日期。

My table structure looks a bit like this:

我的表结构看起来有点像这样:

----------------------------------
| id | added | page | sessionid
----------------------------------

Any ideas?

有任何想法吗?

My query gives me results that look like this:

我的查询给了我看起来像这样的结果:

2010-11-24 | 2
2010-11-24 | 14
2010-11-24 | 17
2010-11-24 | 1

2010-11-24 | 2 2010-11-24 | 14 2010-11-24 | 17 2010-11-24 | 1

While I'd be hoping for something more like a total of all those under the 1 date, ie:

虽然我希望有更多类似于1日期以下所有人的东西,即:

2010-11-24 | 34

2010-11-24 | 34

1 个解决方案

#1


3  

Each date contains the time which will be different for each request. If you use DATE in the GROUP BY clause just like you did in the SELECT clause, that will solve your problem.

每个日期包含每个请求的不同时间。如果您在GROUP BY子句中使用DATE,就像在SELECT子句中一样,这将解决您的问题。

By grouping by sessionID, it's going to create a row for every session. If instead of grouping by sessionID, you use COUNT(DISTINCT sessionID), that will contact the distinct number of session IDs for that date.

通过sessionID分组,它将为每个会话创建一行。如果不是按sessionID分组,而是使用COUNT(DISTINCT sessionID),它将联系该日期的不同数量的会话ID。

SELECT DATE(added) as date, COUNT(DISTINCT sessionID) as sessions FROM visits GROUP BY DATE(added)

#1


3  

Each date contains the time which will be different for each request. If you use DATE in the GROUP BY clause just like you did in the SELECT clause, that will solve your problem.

每个日期包含每个请求的不同时间。如果您在GROUP BY子句中使用DATE,就像在SELECT子句中一样,这将解决您的问题。

By grouping by sessionID, it's going to create a row for every session. If instead of grouping by sessionID, you use COUNT(DISTINCT sessionID), that will contact the distinct number of session IDs for that date.

通过sessionID分组,它将为每个会话创建一行。如果不是按sessionID分组,而是使用COUNT(DISTINCT sessionID),它将联系该日期的不同数量的会话ID。

SELECT DATE(added) as date, COUNT(DISTINCT sessionID) as sessions FROM visits GROUP BY DATE(added)