title: MYSQL: Last time a user was active within a time period
body: <p>I have a JS sending back a log to a PHP page to insert an entry to a MYSQL DB in the following format:</p>
  
  <pre><code>id      ipad       uid  ts                  urlpath  
  1455    1.1.1.15    13  19/01/2019 07:32    http://1.1.1.151/useradmin.php  
  1456    1.1.1.15    13  19/01/2019 07:33    http://1.1.1.151/useradmin.php  
  1460    1.1.1.15    13  19/01/2019 07:37    http://1.1.1.151/useradmin.php  
  1461    1.1.1.15    19  19/01/2019 08:05    http://1.1.1.151/index.php  
  1462    1.1.1.15    19  19/01/2019 08:05    http://1.1.1.151/index.php  
  1463    1.1.1.15    19  19/01/2019 08:05    http://1.1.1.151/index.php  
  1464    1.1.1.15    13  19/01/2019 23:13    http://1.1.1.151/useradmin.php 
  </code></pre>
  
  <p>What I am trying to do is return the latest SINGLE entry for a uid by timestamp(ts) for a given period (e.g. 30 minutes, 6 hours, 30 days etc)
  I have tried several pieces of code but it never returns the latest entry. The code I am using at the moment is:  </p>
  
  <pre><code>SELECT *  
    FROM usertrack  
   WHERE ts &gt;= NOW() - INTERVAL 43830 MINUTE  
   GROUP 
      BY uid  
  HAVING MAX(ts)  
   ORDER 
      BY ts DESC 
  </code></pre>
  
  <p>But this just picks a random entry and not the latest. </p>
  
  <p>Any help would be greatly appreciated, as I've lost a large amount of hair already. </p>