用于按日、周、月、年保存统计数据的数据库结构

2023-11-28数据库问题
5

本文介绍了用于按日、周、月、年保存统计数据的数据库结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我必须按网站用户活动的天数、周数、月数和年数收集统计数据.我是数据库设计阶段,我想正确地完成这个阶段,因为它会让我的编码生活更轻松.

I have to collect statisctics by days, weeks, months and years of user activity for a site. I am the DB design stage and I wanted to do this stage properly since it will make my coding life easier.

我要做的只是在每次活动发生时将数据库中字段中的值加 1.这样我就可以每天、每周、每月和每年拉出日期.我的数据库应该如何构建?抱歉,如果这对大多数人来说是一个简单的问题.如果这种结构可以扩展以便可以分解为其他类别,那也是很棒的.

What I have to do is just simply increment the values in the fields by 1 in the DB each time an activity happens. So then I can pull up the date by each day, each week, each month and year. How should my DB be structured? Apologies if this is a simple question for most. It would also be great if this structure could be extendable so that it can be broken down by other categories.

我遇到的问题是每个月都由更多天组成,而这些日子在每个日历年都发生变化.

The bit am having trouble with is each month is made up of more days and these days change each calender year.

感谢大家的帮助或指导.

Thanks all for any help or direction.

其他信息:Linux 机器,使用 PHP 和 MySQL

Other info: Linux Machine, making use of PHP and MySQL

推荐答案

无需更新每天、每周等的计数,只需在每次活动发生时向表中插入一行,如下所示:

Instead of updating counts per day, week etc. just INSERT a row into a table each time an activity happens like this:

insert into activities (activity_date, activity_info) 
values (CURRENT_TIMESTAMP, 'whatever');

现在您的报告非常简单,例如:

Now your reports are very simple like:

select count(*) from activities
where activity_date between '2008-01-01' and '2008-01-07';

select YEARWEEK(`activity_date`) as theweek, count(*)
group by theweek

这篇关于用于按日、周、月、年保存统计数据的数据库结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法
ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

MySQL groupwise MAX() 返回意外结果
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)...
2024-04-16 数据库问题
13

MySQL SELECT 按组最频繁
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)...
2024-04-16 数据库问题
16

为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同
Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)...
2024-04-16 数据库问题
13

MySQL GROUP BY DateTime +/- 3 秒
MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)...
2024-04-16 数据库问题
14