简体   繁体   中英

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

Hello guys this is a very simplified version of my table: 在此处输入图片说明

I will like to make four mysql querys to this table. All of them will have to count the total visits where id_user is equal to some particular value and type different from click . One query have to count the visits today, the other in this week, the month and the total visits. Im not very expert on MySQL, i can certainly solve this using PHP but i prefer to put the load on my sql server. Thanks for your help!

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());

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM