MySQL: Count the distinct rows per day

前端 未结 3 1822
庸人自扰
庸人自扰 2020-12-03 03:25

I have an interesting query I need to do. I have a table with an INT column containing ip address numbers (using INET_ATON), and a timestamp<

3条回答
  •  臣服心动
    2020-12-03 03:39

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

    select
        count(*) as count,
        date(timestamp) as date
    from
        tablename
    where
        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 date
    from
        tablename
    where
        timestamp >= date_sub(curdate(), interval 7 day)
    group by 
        ipNum, date;
    
    

提交回复
热议问题