title: Why would this SQL error with "Invalid use of group function"?
body: <p>This is a simple query ran when the user presses <em>logout</em> from my website</p>
  
  <pre><code>UPDATE `user_logins` 
     SET `active` = 0 
   WHERE `user_id` = 3 
     AND `datetime` = MAX(`datetime`) LIMIT 1
  </code></pre>
  
  <p>The <code>user_id</code> value is binded in there with PDO.</p>
  
  <p>I get the following exception thrown</p>
  
  <blockquote>
    <p>Invalid use of group function</p>
  </blockquote>
  
  <p><a href="http://www.google.com/search?ie=UTF-8&amp;oe=UTF-8gfns=1&amp;q=Invalid+use+of+group+function" rel="nofollow noreferrer">Googling around</a> seems to say that it is because I am using an aggregate function in a <code>WHERE</code> clause.</p>
  
  <p>I also found this <a href="https://stackoverflow.com/questions/2330840/mysql-invalid-use-of-group-function">question on Stack Overflow</a>, but playing around with <code>HAVING</code> didn't seem to work for me. I tried replacing the <code>AND</code> with <code>HAVING</code>.</p>
  
  <p>How can I change this query to not use the aggregate (or to use <code>HAVING</code>), but still perform the same functionality?</p>
  
  <p>Thanks a bunch!</p>