繁体   English   中英

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

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

大家好,这是我的表格的非常简化的版本: 在此处输入图片说明

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

放手去吧。 我不知道您的表格叫什么,所以我将其称为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());

您可能想看看这个页面:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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