create table daily_hit_counter(

day date not null,

slot tinyint unsigned not  null,

cnt int unsigned not null,

primary key (day,slot)

)engine=innodb

 

insert into daily_hit_counter(day,slot,cnt) values(CURRENT_DATE,RAND()*100,1) ON DUPLICATE KEY UPDATE CNT=CNT+1;

DUPLICATE KEY UPDATE  的作用是当他的主键(day,slot)存在时,就修改数据,不存在则添加数据;

 

如果希望减少表的行数,以避免表变得太大,可以写个周期执行任务,合并所有结果到每个日期的第一条数据,然后删除每个日期的其他数据;

update daily_hit_counter as c inner join (select day,sum(cnt) as cnt, min(slot) as mslot from daily_hit_counter group by day ) as x using(day)

 set c.cnt=if(c.slot=x.mslot,x.cnt,0),c.slot=if(c.slot=x.mslot,0,c.slot);

 

delete from daily_hit_counter where slot <> 0 and cnt=0;

统计某天的点击数

select sum(cnt) from daily_hit_counter  where date=日期

相关文章:

  • 2021-05-15
  • 2022-12-23
  • 2021-07-05
  • 2022-12-23
  • 2021-08-05
  • 2022-12-23
  • 2022-12-23
  • 2021-07-18
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-05
  • 2022-02-05
  • 2021-10-01
  • 2021-06-25
相关资源
相似解决方案