title: Query diff from previous matching row, with WHERE clause
body: <p>To diff a row with a previous row, I am <code>JOIN</code>ing a table to itself and comparing with the previous <code>id</code> (primary key):</p>
  
  <pre><code>CREATE TABLE `tablename` (
      id INT(5) PRIMARY KEY AUTO_INCREMENT,
      data INT
  );
  
  SELECT t1.id, t1.data, t1.data-(SELECT data FROM tablename WHERE id=t1.id-1) AS diff
  FROM tablename t1 LEFT JOIN tablename t2 ON t1.id = t2.id
  ORDER BY diff DESC LIMIT 1;
  </code></pre>
  
  <p>How can I restrict this to only a particular subset of the data? For instance, if a column <code>user VARCHAR(32)</code> is added to the table, how might I restrict the query to only diff the particular user? Adding a simple WHERE clause to the two <code>SELECT</code> queries won't work as the whole premise depends on sequential <code>id</code>s. I suppose that I could move the data into a temporary table but if there is a cleaner, more efficient solution then I would use that.</p>
  
  <p>My intention is to add a <code>WHERE user='someUser'</code> clause, for which the diff will diff only for a particular user, not the whole table.</p>
  
  <p>Here is a working <a href="http://www.sqlfiddle.com/#!2/a1bd79/1" rel="nofollow">sqlfiddle</a>. I would like to modify this query so that it works only for <code>WHERE name='a'</code>, so that the result would be <code>id=9, data=305, diff=125</code>.</p>