Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL Query for Returning New Visitors from Database
    primarykey
    data
    text
    <p>Suppose I have a table that logs incoming users where each user has an IP address (ipaddr).</p> <p>What's the best way to select all users that have never come to the site before? (so that particular IPADDR value only exists in the table <strong>once</strong> ), however I only want to know about the new visitors that came in the last 6 hours.</p> <p>I basically want to do something like this in SQL:</p> <pre><code>SELECT * from visitors GROUP BY ipaddr WHERE COUNT(ipaddr) = 1 and date &gt; '2011-03-31 00:59:11' </code></pre> <p>HOWEVER, the DATE condition should only apply to the results, and not for checking if the visitors is new or not.</p> <p>UPDATE: </p> <p>There is an SID field that accounts for user browsing sessions.</p> <p>Here's the relevant table schema: </p> <pre><code>CREATE TABLE `visitors` ( `date` timestamp NOT NULL default CURRENT_TIMESTAMP, `sid` bigint(12) unsigned NOT NULL, `ipaddr` int(8) NOT NULL, ) </code></pre> <p>Some example data:</p> <pre><code>INSERT INTO `visitors` (`date`,`sid`, `ipaddr`) VALUES ('2011-03-31 06:25:48', 299521885457, -1454342140); INSERT INTO `visitors` (`date`,`sid`, `ipaddr`) VALUES ('2011-03-31 06:26:37', 299521885457, -1454342140); INSERT INTO `visitors` (`date`,`sid`, `ipaddr`) VALUES ('2010-01-01 15:23:44', 694387538590, -1454342140); </code></pre> <p>This visitor has two rows for his current session happening in realtime, each row is for each page he has visited (only relevant schema shown). The last example row shown is a visit from 2010, which means this ip address has 2 different SIDs belonging to it, therefore is not a new visitors.</p> <p>The query's result should have none of the rows listed above, seeing as how this visitor has two sessions in the database. If the last row is removed (with sid 694387538590), the visitor should become a new visitor and appear in the query.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload