Get Count of Records by Day in MySql

Get Count of Records by Day

This example shows you how to get the count of records within the last 7 days.  In our table, we have the full date and time; Here we show counts, grouped by day.

SELECT
  COUNT(id) AS count_id,
  date(created) as date
  from
      visitor_log
  where
      created >= date_sub(curdate(), interval 7 day)
  group by 
      date;

 

Advanced Example: Taking the above a little further, here's another example, where we get count of ID and count if IP Addresses, so we can see the two together:

SELECT
  COUNT(id) AS view_count,
  COUNT(DISTINCT(ip_addr)) AS count_ips,
  date(created) as date
  from
      visitor_log
  where
      created >= date_sub(curdate(), interval 7 day)
  group by 
      date;

Share this Post