怎么看一个人每个月来了多少次(SQL)

How can I see how many times a person came per month (SQL)

对于我的系统,我想知道访客来我店的次数。我有 wifi 传感器,它们有很多地址,我想知道访客一个月来了多少次

This is the database I use (time is in unix time and get fixed with FROM_UnixTime(sensordata1.time)

所以我想得到的是一个包含上个月访问次数的地址。(每天而不是每个地址所以如果他一天来 5 次就算作 1)

您想查看 2017 年 3 月。因此在 WHERE 子句中将您的结果限制为 2017 年 3 月。您希望每个访问者(地址)一个结果行。所以 GROUP BY 地址。你只想每天数一次。所以 COUNT DISTINCT 天。

select 
  address,
  count(distinct from_unixtime(sensordata1.time, '%Y-%m-%d'))
from sensordata1 
where from_unixtime(sensordata1.time, '%Y-%m') = '2017-03'
group by address;

如果你希望这个更灵活,即执行查询时始终是最后一个月而不是 2017-03 固定的,那么找到今天,减去一个月,并得到这样的月份。