title: MySQL WHERE timestamp >= SUBDATE(MAX(timestamp), INTERVAL 5 DAY)
body: <p>What's the best way to express this in one SQL query?</p>
  
  <p>"Select a few random items that fall within x days of the newest item in the table."</p>
  
  <p>I tried the following:</p>
  
  <pre><code>SELECT *
  FROM table
  HAVING `timestamp` &gt;= SUBDATE(MAX(`timestamp`), INTERVAL 5 DAY)
  ORDER BY RAND()
  LIMIT 10
  </code></pre>
  
  <p>But this only gives me a single result, not 10. <code>WHERE</code> instead of <code>HAVING</code> doesn't cut it because of the use of <code>MAX()</code>.</p>