title: MySQL - COUNT and MAX in same query. MAX ignoring WHERE clause
body: <p>I am executing the following script: </p>
  
  <pre><code>SELECT  a.*,
      gg.image as 'recent_image_file' 
  FROM 
  
  (   SELECT  c.cid, 
              c.category_name AS 'cat_name',
              COUNT(g.id) AS image_count,
              c.category_image AS 'images',
              c.estgendpref,
              MAX(n.id) as 'recent_image'
      FROM tbl_category c
      LEFT JOIN tbl_gallery_category g 
          ON c.cid=g.cat_id
      LEFT JOIN tbl_gallery n 
          ON n.id=g.image_id
  
      WHERE n.date_active &lt; NOW()
      GROUP BY c.cid
      ORDER BY c.category_name ASC
  ) a
  
  INNER JOIN tbl_gallery gg
      ON a.recent_image = gg.id;
  </code></pre>
  
  <p>Everything is returning fine except the MAX(n.id) ignores the WHERE clause and returns the top id for the category. How do I get the MAX(n.id) to return the max for the resultset including the WHERE clause? Thanks.</p>