title: MySQL GROUP BY DateTime +/- 3 seconds
body: <p>Suppose I have a table with 3 columns:</p>
  
  <ul>
  <li>id (PK, int)</li>
  <li>timestamp (datetime)</li>
  <li>title (text)</li>
  </ul>
  
  <p>I have the following records:</p>
  
  <pre><code>1, 2010-01-01 15:00:00, Some Title
  2, 2010-01-01 15:00:02, Some Title
  3, 2010-01-02 15:00:00, Some Title
  </code></pre>
  
  <p>I need to do a GROUP BY records that are within 3 seconds of each other.  For this table, rows 1 and 2 would be grouped together.</p>
  
  <p>There is a similar question here:  <a href="https://stackoverflow.com/questions/4289345/mysql-datetime-group-by-15-mins">Mysql DateTime group by 15 mins</a></p>
  
  <p>I also found this:  <a href="http://www.artfulsoftware.com/infotree/queries.php#106" rel="noreferrer">http://www.artfulsoftware.com/infotree/queries.php#106</a></p>
  
  <p>I don't know how to convert these methods into something that will work for seconds.  The trouble with the method on the SO question is that it seems to me that it would only work for records falling within a bin of time that starts at a known point.  For instance, if I were to get <code>FLOOR()</code> to work with seconds, at an interval of 5 seconds, a time of 15:00:04 would be grouped with 15:00:01, but not grouped with 15:00:06.</p>
  
  <p>Does this make sense?  Please let me know if further clarification is needed.</p>
  
  <p><strong>EDIT:</strong> For the set of numbers, {1, 2, 3, 4, 5, 6, 7, 50, 51, 60}, it seems it might be best to group them {1, 2, 3, 4, 5, 6, 7}, {50, 51}, {60}, so that each grouping row depends on if the row is within 3 seconds of the previous.  I know this changes things a bit, I'm sorry for being wishywashy on this.</p>
  
  <p>I am trying to fuzzy-match logs from different servers.  Server #1 may log an item, "Item #1", and Server #2 will log that same item, "Item #1", within a few seconds of server #1.  I need to do some aggregate functions on both log lines.  Unfortunately, I only have title to go on, due to the nature of the server software.</p>