大家好,这是我表格的简化版本:
我想对这个表做四个mysql查询。所有这些都必须计算 id_user 等于某些特定值且类型与点击不同的总访问次数>。一个查询必须计算今天的访问次数,另一个查询必须计算本周,月份和总访问次数。我不是MySQL的专家,我当然可以使用PHP解决这个问题,但我更喜欢把负载放在我的sql server上。谢谢你的帮助!
答案 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()
以及其他一些功能应该可以解决问题