Jump to content

Length of time


Canman2005

Recommended Posts

Hi all

 

I'm building a very simple stats package for a website im doing, currently I hold the

 

Time

Date

URL

IP Address

 

Each time someone goes onto a page, then the above details are captured and stored in an sql database.

 

What I want to do is to store the length of time someone has been on each page, so as well as capturing the "Time", "Date", "URL" & "IP Address", I want to capture "Length" and store this like 00:04:23.

 

Is it possible to store the length of time someone has been on a page?

 

Thanks in advance

 

Ed

Link to comment
Share on other sites

Here's one way:

mysql> DESCRIBE temp;
+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| ts    | int(10) unsigned | YES  |     | NULL    |       |
| url   | varchar(255)     | YES  |     | NULL    |       |
| ip    | varchar(64)      | YES  |     | NULL    |       |
+-------+------------------+------+-----+---------+-------+
3 rows in set (0.02 sec)

mysql> SELECT * FROM temp;
+------------+---------------------------------------------------------------+-------------+
| ts         | url                                                           | ip          |
+------------+---------------------------------------------------------------+-------------+
| 1185903285 | http://www.example.com/index.php                              | 127.0.0.1   |
| 1185903297 | http://www.example.com/~dingo/ate-my-baby.html                | 127.0.0.1   |
| 1185903381 | http://www.example.com/~dingo/about.html                      | 127.0.0.1   |
| 1185903469 | http://www.example.com/store/dingo-traps.php                  | 127.0.0.1   |
| 1185903200 | http://www.example.com/splash.aspx                            | 127.0.0.1   |
| 1185903270 | http://www.example.com/splash.aspx                            | 192.168.0.1 |
| 1185903310 | http://www.example.com/index.php                              | 192.168.0.1 |
| 1185903315 | http://www.example.com/~mp3s/index.html                       | 192.168.0.1 |
| 1185903355 | http://www.example.com/~mp3s/Dream%20Theater/index.html       | 192.168.0.1 |
| 1185903382 | http://www.example.com/~mp3s/Dream%20Theater/Awake/index.html | 192.168.0.1 |
| 1185903333 | http://www.example.com/store/shopping_cart.php                | 127.0.0.1   |
+------------+---------------------------------------------------------------+-------------+
11 rows in set (0.00 sec)

mysql> SET @a:=0,@b:=0; SELECT a.url,SEC_TO_TIME(AVG(b.ts-a.ts)) AS "Average Duration",COUNT(*) AS Hits
FROM
(SELECT url,ip,@a:=@a+1 AS count,ts FROM temp ORDER BY ip,ts) a 
JOIN (SELECT ip,@b:=@b+1 AS count,ts FROM temp ORDER BY ip,ts) b 
ON a.ip=b.ip AND b.count=a.count + 1
GROUP BY a.url;
Query OK, 0 rows affected (0.00 sec)

+---------------------------------------------------------+------------------+------+
| url                                                     | Average Duration | Hits |
+---------------------------------------------------------+------------------+------+
| http://www.example.com/index.php                        | 00:00:08         |    2 |
| http://www.example.com/splash.aspx                      | 00:01:02         |    2 |
| http://www.example.com/store/shopping_cart.php          | 00:00:48         |    1 |
| http://www.example.com/~dingo/about.html                | 00:01:28         |    1 |
| http://www.example.com/~dingo/ate-my-baby.html          | 00:00:36         |    1 |
| http://www.example.com/~mp3s/Dream%20Theater/index.html | 00:00:27         |    1 |
| http://www.example.com/~mp3s/index.html                 | 00:00:40         |    1 |
+---------------------------------------------------------+------------------+------+
7 rows in set (0.00 sec)

 

Don't store date and time separately.  Use either an INT UNSIGNED and store as a Unix timestamp or use DATETIME.  Also, storing IPs as integers is more efficient (although I didn't do that in the example).  See INET_ATON() in the MySQL manual.

Link to comment
Share on other sites

thanks for that. I have tried your suggestion, but I get the error

 

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"Av (SELECT url,ip,@a:=@a+1 AS count,ts FROM temp ORDER BY ip,ts) a JOIN (SELECT' at line 1

 

What am I doing wrong?

 

Thanks

 

Ed

Link to comment
Share on other sites

When you copy and pasted the above, did you get all of the query?  cause it looks like you didn't.

 

SET @a:=0,@b:=0; 
SELECT a.url,SEC_TO_TIME(AVG(b.ts-a.ts)) AS "Average Duration",COUNT(*) AS Hits
FROM
(SELECT url,ip,@a:=@a+1 AS count,ts FROM temp ORDER BY ip,ts) a 
JOIN (SELECT ip,@b:=@b+1 AS count,ts FROM temp ORDER BY ip,ts) b 
ON a.ip=b.ip AND b.count=a.count + 1
GROUP BY a.url

Link to comment
Share on other sites

  • 5 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.