简体   繁体   English

如何在 MYSQL 中获取过去 30 天的数据并使用 php 中的变量在循环内打印过去 30 天

[英]How to get data in past 30 days in MYSQL and Print Past 30 days inside a loop using a variable in php

I need to get past 30 days sales records from MYSQL database to display in a bar chart how many items sold each day.我需要从 MYSQL 数据库中获取过去 30 天的销售记录,以在条形图中显示每天售出的商品数量。 Also I need to display past 30 days in my chart x axis in my php document.我还需要在我的 php 文档中的图表 x 轴上显示过去 30 天。 I tried run this code inside a for loop.我尝试在 for 循环中运行此代码。 because I am running some other codes to display some data using this loop.因为我正在运行一些其他代码来使用这个循环显示一些数据。 Take a look at the following code and help me how to achieve this.看看下面的代码并帮助我如何实现这一点。 This is the table I try to get data from这是我尝试从中获取数据的表


Date format (YYYY-MM-DD)

Products Table

product_id | sold_by | qty | added_date  | sold_date
-----------+---------+-----+-------------+-----------
3          | 12      | 7   | 2022-05-05  | 2022-07-28
3          | 12      | 7   | 2022-05-05  | 2022-07-29
3          | 12      | 7   | 2022-05-05  | 2022-07-30
3          | 12      | 1   | 2022-05-05  | 2022-07-30
3          | 12      | 2   | 2022-05-05  | 2022-07-30
6          | 22      | 4   | 2022-06-06  | 2022-07-31
8          | 11      | 6   | 2022-08-05  | 2022-07-31



Deleted Table

product_id | added_date  | delete_date
-----------+-------------+-----------
3          | 2022-05-05  | 2022-07-28
3          | 2022-05-05  | 2022-07-29
3          | 2022-05-05  | 2022-07-30
3          | 2022-05-05  | 2022-07-30
3          | 2022-05-05  | 2022-07-30
6          | 2022-06-06  | 2022-07-31
8          | 2022-08-05  | 2022-07-31

As you can see for some days I have multiple records.正如你所看到的,我有多个记录。

<?php
//for loop to run 30 times to get 30 days results
for ($i = 30; $i >= 0; $i--) {
   $sold_products_count = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM products WHERE sold_date = CURRENT_DATE - $i"));
   $deleted_count = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM deleted WHERE delete_date = CURRENT_DATE - $i"));
?> 
{
<?php
   $current_date = date("Y-m-d");
   $new_date = date_create($current_date);
   date_sub($new_date, date_interval_create_from_date_string("$i day"));
?>
   x: '<?php echo date_format($new_date, "Y-m-d"); ?>',
   a: <?php echo $deleted_count; ?>,
   s: <?php echo $sold_products_count; ?>,
},
<?php
}
?>

x- X axis data in the chart a - number of products deleted in specific date s - number of products sold in specific date x- 图表中的 X 轴数据 a - 特定日期删除的产品数量 s - 特定日期销售的产品数量

With the above code I was able to print the x axis data.使用上面的代码,我能够打印 x 轴数据。 I am not sure how to get past 30 days data from the SQL database.我不确定如何从 SQL 数据库中获取过去 30 天的数据。 I think the problem is in the $i part in the SQL query.我认为问题出在 SQL 查询中的 $i 部分。

In your query, if you use INTERVAL , you can use some days for adding/reducing from the current or specific date.在您的查询中,如果您使用INTERVAL ,您可以使用一些天数来添加/减少当前或特定日期。 For example in your case, if you always want to get the last 30 days data, you can use your WHERE like this:例如,在您的情况下,如果您总是想获取最近 30 天的数据,您可以像这样使用 WHERE:

sold_date >= CURDATE() - INTERVAL 30 DAY

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

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