2012-01-04 50 views
1

好傢伙,這是我的表的一個非常簡化的版本: enter image description here伯爵訪問,本週,上月和總計[MySQL查詢]

我會想提出四個的MySQL querys此表。他們都將有總訪問人數,其中id_user等於某個特定值點擊不同。一個查詢必須計算今天的訪問次數,本週的訪問次數,該月的訪問次數和總訪問次數。我不是MySQL的專家,我當然可以使用PHP解決這個問題,但我更喜歡把負載放在我的SQL服務器上。謝謝你的幫助!

+0

你是指當前的周/月還是一週/月內? – 2012-01-04 08:42:45

回答

9

給這些一展身手。我不知道你的桌子叫什麼,所以我把它推薦爲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());