I’m currently getting a users visit count based on a timestamp but I would also like to include the total number of visits. Is this possible to do with one MySQL statement. Here is my current query that returns only one count based on a timestamp:
$sql = "SELECT count(*) as visitors_count FROM profile_visits
LEFT JOIN users on users.user_id=profile_visits.user_id
LEFT JOIN profile_info ON profile_info.user_id = users.user_id
WHERE profile_user_id=5
AND users.restricted <> 1
AND profile_visits.visit_time > '2012-07-30 18:53:16'";
Can I also return the total number of visits that exclude the timestamp?
You can use a
CASEexpression to conditionally aggregate withinCOUNT():I simply moved the
> '2012-07-30 18:53:16'from theWHEREclause to theCASEexpression withinCOUNT()which will only count the row if it meets that criteria. And of courseCOUNT(*)will count all rows regardless of the timestamp.