Count visits by today, this week, last month and total [MySQL query]

后端 未结 2 1153
走了就别回头了
走了就别回头了 2021-02-11 09:24

Hello guys this is a very simplified version of my table: \"enter

I will like to make four

相关标签:
2条回答
  • 2021-02-11 10:08

    You might want to have a look at this page:

    http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

    The functions month(), date(), curdate(), week() and a few others should do the trick

    0 讨论(0)
  • 2021-02-11 10:09

    Give these a go. I don't know what your table is called so I have referrered to it as trafficTable:

    -- Visits today
    select count(*) as visits_today
    from trafficTable tt
    where tt.type != 'click'
    and tt.id_user = '19d71'
    and datetime >= curdate();
    
    -- Visits this week
    select count(*) as visits_this_week
    from trafficTable tt
    where tt.type != 'click'
    and tt.id_user = '19d71'
    and yearweek(datetime) = yearweek(curdate());
    
    -- Visits this month
    select count(*) as visits_this_month
    from trafficTable tt
    where tt.type != 'click'
    and tt.id_user = '19d71'
    and year(datetime) = year(curdate())
    and month(datetime) = month(curdate());
    
    -- Total visits
    select count(*) as total_visits
    from trafficTable tt
    where tt.type != 'click'
    and tt.id_user = '19d71';
    
    --- if you want the last month - this help other ppl in other thread
        select count(*) as visits_this_month
        from trafficTable tt
        where tt.type != 'click'
        and tt.id_user = '19d71'
        and year(datetime) <= year(curdate())
        and month(datetime) <= month(curdate());
    
    0 讨论(0)
提交回复
热议问题