title: How to make variable interval condition query with join
body: <p>I have two MySQL tables. </p>
  
  <pre><code>test_settings 
  id, interval_in_minutes, other_irrevelant_settings
  
  test_results
  id, test_settings_id(Foregin Key), datetime, value
  </code></pre>
  
  <p>I would like to run query <code>every 1 minute</code> and run tests which last result was later than <code>interval_in_minutes</code> for every individual test.</p>
  
  <p>Sample db data</p>
  
  <pre><code>test_settings
  id interval_in_minutes
  1 10 
  2 40
  3 140
  etc
  </code></pre>
  
  <p>So finally result of query will be <code>id 3</code> if there was no result inserted for last 140 minutes (based on <code>test_result</code> <code>datetime</code> inserted after test).</p>
  
  <pre><code>SELECT ts.id
  FROM test_settings ts
  WHERE (SELECT tr.date
         FROM test_results tr
         WHERE tr.test_settings_id = tr.id
         ORDER BY tr.datetime DESC
         LIMIT 1) &lt; DATE_SUB(NOW(), INTERVAL XXXXX MINUTES);
  </code></pre>
  
  <p>It works only if interval is like <code>XXXXX = 4</code> but I need it to be dynamic like <code>XXXXX = ts.interval_in_minutes</code>.</p>
  
  <p>No idea how to do it.. Any help?</p>