MySQL: Count the distinct rows per day MySQL: Count the distinct rows per day sql sql

MySQL: Count the distinct rows per day


SELECT  DATE(timestamp) Date, COUNT(DISTINCT ipNum) totalCOuntFROM    tableNameGROUP   BY  DATE(timestamp)


Here's how you'd get counts per day for the last 7 days:

select    count(*) as count,    date(timestamp) as datefrom    tablenamewhere    timestamp >= date_sub(curdate(), interval 7 day)group by     date;+-------------+------------+| count       | date       |+-------------+------------+| #forThatDay | 2020-02-21 || #forThatDay | 2020-02-22 || #forThatDay | 2020-02-22 || #forThatDay | 2020-02-23 || #forThatDay | 2020-02-24 || #forThatDay | 2020-02-25 || #forThatDay | 2020-02-26 |+-------------+------------+7 rows in set (0.03 sec)

group by the ipNum column first to get distinct counts of that column per day:

select    count(*) as count,    date(timestamp) as datefrom    tablenamewhere    timestamp >= date_sub(curdate(), interval 7 day)group by     ipNum, date;


$log_date     = date('Y-m-d H:i:s');$log_date     = date('Y-m-d H:i:s', strtotime($log_date.' -1 hour'));SELECT ipNum, COUNT(ipNum), COUNT(DISTINCT ipNum), DATE(timestamp), timestamp FROM tableName  WHERE `timestamp` > '".$log_date."' GROUP BY ipNum ORDER BY DATE(timestamp) DESC  

THIS WILL GIVE YOU A RESULT LIKE

 ip                  TIME               COUNTIPS11.237.115.30     2018-01-27 19:13:51       121.744.133.52     2018-01-27 19:14:03       144.628.197.51     2018-01-27 19:48:12       14