title: MYSQL last login and number of logins in last 3 months
body: <p>I'm trying to write a query to join a user table to an activity logging table and return the following for each user:</p>
  
  <p>A)  The time they last logged in.</p>
  
  <p>B)  The number of logins in the last 3 months.</p>
  
  <p>This is what I have come up with so far:</p>
  
  <pre><code>SELECT A.UserID, COUNT( Activity ) AS Logins, MAX( TIME ) AS LastLogin  
  FROM UserMaster A  
  LEFT JOIN UserWebActivity B ON A.UserID = B.UserID  
  AND Activity =  'Login'  
  AND TIME BETWEEN DATE_SUB( NOW( ) , INTERVAL 3 MONTH ) AND NOW( )  
  GROUP BY A.UserID  
  </code></pre>
  
  <p>This almost works, but it doesn't return the latest login for any user that hasn't logged in within the last 3 months.  How can I get count() and max() to work together properly?</p>