title: MySQL max() - not returning max() value
body: <p>I have a query which I want to return the highest id value in <code>idevent</code> that is within a given range of sensor ID's (<code>sensorID</code>) however the query is not returning the highest value.</p>
  
  <p>The results when I run the query minus the <code>max()</code> statment:</p>
  
  <pre><code>mysql&gt; SELECT * FROM events WHERE timestamp BETWEEN "2015-03-09 10:45:35" - INTERVAL 4000 SECOND AND "2015-03-09 10:45:35" AND (sensorID = 34035434 OR sensorID = 34035492 OR sensorID = 34035426 OR sensorID = 34035482 OR sensorID = 34035125 OR sensorID = 34035498 OR sensorID = 34035508 OR sensorID = 34035444 OR sensorID = 34035418 OR sensorID = 34035466 OR sensorID = 34035128 OR sensorID = 34035119 OR sensorID = 34035448 OR sensorID = 34037294 OR sensorID = 34035549);
  +---------+---------------------+----------+-------+
  | idevent | timestamp           | sensorID | event |
  +---------+---------------------+----------+-------+
  |  117794 | 2015-03-09 10:14:58 | 34035434 |     9 |
  |  117795 | 2015-03-09 10:15:03 | 34035508 |     9 |
  |  117796 | 2015-03-09 10:15:17 | 34035508 |     1 |
  +---------+---------------------+----------+-------+
  3 rows in set (0.00 sec)
  </code></pre>
  
  <p>The results given when the <code>max()</code> statement is added as a <code>HAVING</code> clause:</p>
  
  <pre><code>mysql&gt; SELECT * FROM events WHERE timestamp BETWEEN "2015-03-09 10:45:35" - INTERVAL 4000 SECOND AND "2015-03-09 10:45:35" AND (sensorID = 34035434 OR sensorID = 34035492 OR sensorID = 34035426 OR sensorID = 34035482 OR sensorID = 34035125 OR sensorID = 34035498 OR sensorID = 34035508 OR sensorID = 34035444 OR sensorID = 34035418 OR sensorID = 34035466 OR sensorID = 34035128 OR sensorID = 34035119 OR sensorID = 34035448 OR sensorID = 34037294 OR sensorID = 34035549) HAVING max(idevent);
  +---------+---------------------+----------+-------+
  | idevent | timestamp           | sensorID | event |
  +---------+---------------------+----------+-------+
  |  117794 | 2015-03-09 10:14:58 | 34035434 |     9 |
  +---------+---------------------+----------+-------+
  1 row in set (0.00 sec)
  </code></pre>
  
  <p>Why is this not returning the lastest value?</p>