计算今天,本周,上个月和[MySQL查询]的访问次数

时间:2012-01-04 08:35:55

标签: php mysql

大家好,这是我表格的简化版本: enter image description here

我想对这个表做四个mysql查询。所有这些都必须计算 id_user 等于某些特定类型点击不同的总访问次数>。一个查询必须计算今天的访问次数,另一个查询必须计算本周,月份和总访问次数。我不是MySQL的专家,我当然可以使用PHP解决这个问题,但我更喜欢把负载放在我的sql server上。谢谢你的帮助!

2 个答案:

答案 0 :(得分: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());

答案 1 :(得分:3)

您可能需要查看此页面:

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

功能month()date()curdate()week()以及其他一些功能应该可以解决问题