title: Subtracting timestamps in MySQL
body: <p>I have the following select statement where I subtract timestamps for knowing how long a truck has been stopped at a location:</p>
  
  <pre><code> SELECT  f.id, f.imei as imei, f.speed as speed  FROM stops f, stops f2
   where f2.id = f.id-1
   and f.imei = 7466
   and hour(timediff(f2.timestamp,f.timestamp)) * 3600 +   minute(timediff(f2.timestamp,f.timestamp)) * 60 + second(timediff(f2.timestamp,f.timestamp)) &gt; 240 
   and f.speed = 0
   and f2.timestamp &gt;= '2013-08-20 00:00:00' 
   and f2.timestamp &lt;= '2013-08-20 23:59:59'  
   order by f2.timestamp desc
  </code></pre>
  
  <p>The rows for calculating the stops:</p>
  
  <pre><code>id imei     timestamp               speed
  1  7466     2013-08-20 13:19:00     30    
  2  7466     2013-08-20 13:20:00     0 
  3  7466     2013-08-20 13:24:30     20
  </code></pre>
  
  <p>So the select gives as result that there was a stop of 4 minutes for the vehicle 7466.</p>
  
  <p>The problem comes when the rows are like this:</p>
  
  <pre><code>id imei     timestamp               speed 
  1  7466     2013-08-20 13:19:00     30     
  2  7466     2013-08-20 13:20:00     0 
  3  7466     2013-08-20 13:21:00     0 
  4  7466     2013-08-20 13:22:00     0 
  5  7466     2013-08-20 13:24:30     20
  </code></pre>
  
  <p>It does not subtract.  How can I get the resulting time?</p>