简体   繁体   English

找出上周数据的差异

[英]Find difference in last weeks data

I'm trying to find out how much a person's views have increased in a week.我试图找出一个人的观点在一周内增加了多少。

The columns I'm working with look like this.我正在处理的列看起来像这样。

在此处输入图片说明

Code to retrieve and display this data检索和显示此数据的代码

<?php
  $query = "SELECT post_title, post_date, post_views_count FROM posts 
  WHERE post_user = '{$username}' AND post_status = 'published' ORDER 
  BY post_views_count DESC LIMIT 8";

  $select_views = mysqli_query($connection, $query);

  while ($row = mysqli_fetch_assoc($select_views)) {
    $post_title = escape($row['post_title']);
    $post_date = escape($row['post_date']);
    $post_views_count = escape($row['post_views_count']);

    echo $post_title . ' / ' . $post_views_count . ' / ' . $post_date . 
    '<br><br>';
  }
?>

Returns data looking like this返回看起来像这样的数据

在此处输入图片说明

Now how am I able to find the difference in how much the post_views_count increased in the past 7 days?现在我如何才能找到过去 7 天内 post_views_count 增加的差异?

I'm ideally trying to echo something like "Your post has 55 more views this week"理想情况下,我试图回应诸如“您的帖子本周还有 55 次观看”之类的内容

Any direction is greatly appreciated.任何方向都非常感谢。

Say for this week you calculated, that all the post has 0 views, then store that amount in a separate column for particular posts.假设本周您计算出所有帖子的浏览量为 0,然后将该数量存储在特定帖子的单独列中。 And then keep on updating only the post_views column for a day or week, if you are trying to show the 1 week difference regularly.然后继续只更新一天或一周的post_views列,如果您想定期显示 1 周的差异。

Then, you can calculate the views and subtract last stored views from post_views to get the difference.然后,您可以计算视图并从post_views减去最后存储的视图以获得差异。

After calculating the difference, update the last_views columns with the posts_views .计算后的差值,更新与该last_views列posts_views

Hope it helps.希望能帮助到你。

I'm not sure if you wanted to do this as per post or for all posts.我不确定您是想按帖子还是所有帖子执行此操作。

This approach is for all posts.这种方法适用于所有帖子。

You could find the limits for the current week (in this example from Monday to Sunday):您可以找到当前周的限制(在本例中从周一到周日):

$startOfTheWeek = date("Y-m-d", strtotime('monday this week'));
$endOfTheWeek = date("Y-m-d", strtotime('sunday this week'));

Then running something like the following query, you get the views before the current week and during the current week:然后运行类似于以下查询的内容,您将获得当前周之前和本周期间的视图:

SELECT SUM(viewsThisWeek) AS viewsThisWeek, SUM(viewsBeforeThisWeek) AS viewsBeforeThisWeek FROM
  (SELECT SUM(post_views_count) AS viewsThisWeek, 0 AS viewsBeforeThisWeek FROM posts AS tbl1
   WHERE post_user = '{$username}' AND (post_date BETWEEN '{$startOfTheWeek}' AND '{$endOfTheWeek}')
UNION ALL
   SELECT '0' AS viewsThisWeek, SUM(post_views_count) AS viewsBeforeThisWeek FROM posts AS tbl2
   WHERE post_user = '{$username}' AND post_date < '{$startOfTheWeek}') AS tbl3

After this you can easily calculate the difference in the views.在此之后,您可以轻松计算视图中的差异。

If you want to have the differences per post, then just delimit the query according to that.如果您希望每个帖子都有差异,那么只需根据此分隔查询即可。

You can make a stored procedure that stores the data from past days and once the data is older then 7 days it drops that data.您可以创建一个存储过程来存储过去几天的数据,一旦数据较旧,则 7 天后它会删除该数据。

This will make it so you can compare data and once the data is out of use it automatically drops it.这将使您可以比较数据,一旦数据不再使用,它​​就会自动删除它。

This will prevent your database from filling up.这将防止您的数据库填满。

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

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