[英]Working with MySQL / MySQLi data - daily, weekly and monthly graphs
I'm developing a dashboard with graphs.我正在开发一个带有图表的仪表板。
What's the problem?有什么问题?
Let's say, that I have a table with the folowing sctructure:假设我有一张具有以下结构的表:
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| total | int | NO | | NULL | |
| new | int | NO | | NULL | |
| date | date | YES | | NULL | |
+-------+------+------+-----+---------+-------+
where total
stands for Total Members and new
for New Members ( date
is a date of course - in format: yyyy-mm-dd
).其中total
代表总成员数, new
代表新成员( date
当然是日期 - 格式: yyyy-mm-dd
)。
Example of columns:列示例:
+-------+-------+------------+
| total | new | date |
+-------+-------+------------+
| 3450 | 21 | 2021-11-06 |
| 3650 | 200 | 2021-11-07 |
| 3694 | 34 | 2021-11-08 |
| 3520 | 26 | 2021-11-09 |
| 3399 | -321 | 2021-11-10 |
| 3430 | 31 | 2021-11-11 |
| 3450 | 20 | 2021-11-12 |
| 3410 | -40 | 2021-11-13 |
| 3923 | 513 | 2021-11-14 |
| 4019 | 96 | 2021-11-15 |
| 4119 | 100 | 2021-11-16 |
| 4000 | -119 | 2021-11-17 |
| 3000 | -1000 | 2021-11-18 |
| 3452 | 452 | 2021-11-19 |
| 3800 | 348 | 2021-11-20 |
| 3902 | 102 | 2021-11-21 |
| 4050 | 148 | 2021-11-22 |
+-------+-------+------------+
And there are a few options, where the dashboard user can select between 2 dates and type of graphs (daily, weekly, monthly).这里面有几个选项,其中仪表盘,用户可以2个日期和图表(每日,每周,每月)的类型之间进行选择。
Image, that describes the Setting options.图像,描述设置选项。
I need to take these 2 dates and somehow get all data from the database between the given term.我需要获取这两个日期,并以某种方式从数据库中获取给定期限之间的所有数据。 Well, but that's not all.嗯,但这还不是全部。 The Daily, Weekly and Monthly option means, that graphs will be showing average newcoming and total members per every week (so if I will grab 7 days from the database, I need to create an average - and do this between all these days / weeks / months in a term), if it's weekly, etc. So the final graph will be showing something like: “每日”、“每周”和“每月”选项意味着,图表将显示每周的平均新成员和总成员(因此,如果我从数据库中获取 7 天,我需要创建一个平均值 - 并在所有这些天/周之间执行此操作/ 一个学期的月数),如果是每周一次,等等。所以最终图表将显示如下内容:
250 new 20 new 31 new
1000 total 1020 total 1051 total
Nov 7 Nov 14 Nov 21
etc...等等...
Ubuntu: 21.04
Ubuntu: 21.04
MySQL: 8.0.27
MySQL: 8.0.27
PHP: 7.4.23
PHP: 7.4.23
Apache: 2.4.46
阿帕奇: 2.4.46
Feel free to ask.随意问。
Does anyone have any ideas, please?请问有人有什么想法吗?
Thanks for the responses,感谢您的回复,
Adalbert阿达尔伯特
I don't get where your numbers come from我不明白你的数字来自哪里
But your query would go like this.但是您的查询会像这样。
For the month you need to group by MONTH
of course对于您当然需要按MONTH
分组的月份
CREATE TABLE members ( `total` INTEGER, `new` INTEGER, `date` date ); INSERT INTO members (`total`, `new`, `date`) VALUES ('3450', '21', '2021-11-06'), ('3650', '200', '2021-11-07'), ('3694', '34', '2021-11-08'), ('3520', '26', '2021-11-09'), ('3399', '-321', '2021-11-10'), ('3430', '31', '2021-11-11'), ('3450', '20', '2021-11-12'), ('3410', '-40', '2021-11-13'), ('3923', '513', '2021-11-14'), ('4019', '96', '2021-11-15'), ('4119', '100', '2021-11-16'), ('4000', '-119', '2021-11-17'), ('3000', '-1000', '2021-11-18'), ('3452', '452', '2021-11-19'), ('3800', '348', '2021-11-20'), ('3902', '102', '2021-11-21'), ('4050', '148', '2021-11-22');
SELECT `new`,sumtotal, `date` FROM members m INNER JOIN (SELECT SUM(`new`) sumtotal, MIN(`date`) mindate FROM members GROUP BY WEEK(`date`)) t1 ON m.`date`= t1.mindate WHERE m.`date` BETWEEN '2021-11-07' AND '2021-11-22'
\nnew |新 | sumtotal |总计 | date日期 \n--: | --: | -------: | -------: | :--------- :---------\n200 | 200 | -50 | -50 | 2021-11-07 2021-11-07\n513 |第513话390 |第390话2021-11-14 2021-11-14\n102 | 102 | 250 | 250 | 2021-11-21 2021-11-21\n
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.