php - 按最近 10 分钟文章浏览量从高到低排序

标签 php mysql sorting join

我在一个页面上有一个从 mysql 获得的文章链接列表,我想对它们进行排序,使过去 10 分钟内浏览次数最多的文章首先显示在其他文章的顶部。目前我正在尝试的是,当有人单击一篇文章时,新行将插入到表“ View ”中,其中包含列 id、时间戳和article_id。我有另一个名为articles 的表,其中包含所有文章数据,并且还具有与 View 表中的任何文章相匹配的article_id 列。

基本上,当单击一篇文章时,它会像这样将行插入到 View 表中。

id  timestamp               article_id
1   2018-03-24 16:02:16     12345
2   2018-03-24 16:02:18     54321
3   2018-03-24 16:02:20     12345
4   2018-03-24 16:02:22     12345
5   2018-03-24 16:02:24     23456
6   2018-03-24 16:02:26     23456
7   2018-03-24 16:02:28     54321
8   2018-03-24 16:02:30     23456
9   2018-03-24 16:02:32     12345
10  2018-03-24 16:02:34     34567
11  2018-03-24 16:02:36     34567
12  2018-03-24 16:02:38     54321
13  2018-03-24 16:02:40     12345
14  2018-03-24 16:02:42     12345
15  2018-03-24 16:02:44     12345

当结果显示在页面上时,我想首先显示点击次数最多的内容,正如我上面所说的。在此示例中,显示的 15 篇文章中有 4 篇文章有浏览量/点击量,因此浏览量表中这 4 篇文章有多行,需要首先从最高到最低进行计数和显示,然后使用其余的正常文章结果像往常一样没有点击显示。如果有意义的话,就像这样。

Results:
article_1 - article_id 12345 - 6 views
article_2 - article_id 54321 - 3 views
article_3 - article_id 23456 - 3 views
article_4 - article_id 34567 - 2 views
article_5 - article_id 11111 - 0 views
article_6 - article_id 22222 - 0 views
article_7 - article_id 33333 - 0 views
article_8 - article_id 44444 - 0 views
article_9 - article_id 55555 - 0 views
article_10 - article_id 66666 - 0 views
article_11 - article_id 77777 - 0 views
article_12 - article_id 88888 - 0 views
article_13 - article_id 99999 - 0 views
article_14 - article_id 111111 - 0 views
article_15 - article_id 222222 - 0 views

我在尝试执行此操作的网站上获得了相当多的流量,因此我试图找出执行查询的最佳方法,并尽可能避免对性能造成重大影响。

我不确定如何查询文章表以获取所有文章信息,然后计数并加入 View 表以获取过去 10 分钟内有 View 的每篇文章的 View 计数,并按 View 对它们进行排序文章第一。我一直在尝试提出一个查询来执行此操作,但它永远不会按照我描述的方式工作。

有人有什么想法或建议吗?谢谢。

编辑:

Articles table:
id, article_id, title, description

Views table:
id, timestamp, article_id

最佳答案

I am unsure how to query the articles table to get all the article info and then count and join the views table to get the view count for each article that had views within the last 10 minutes time period and sort them by the viewed articles first.

您的查询需要类似于下面的这些查询。

SELECT 
   Articles.id
 , COUNT(*) AS views
FROM 
 Articles 
LEFT JOIN 
 Views
ON
     Articles.id = Views.article_id 
   AND
     Views.timestamp >= NOW() - INTERVAL 10 MINUTE
GROUP BY
 Articles.id
ORDER BY
 COUNT(*) DESC

SELECT 
   Articles.id
 , COUNT(*) AS views
FROM 
 Articles
LEFT JOIN 
 Views
ON
     Articles.id = Views.article_id 
   AND
     Views.timestamp >= NOW() - INTERVAL 10 MINUTE
GROUP BY
 Articles.id
ORDER BY
 CASE
  WHEN views >= 1
  THEN 1
  ELSE 2 
 END

关于php - 按最近 10 分钟文章浏览量从高到低排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49467150/

相关文章:

mysql - 如何合并两个不同表中的数据?

java - Tomcat 7.0 连接池 - jdbc 驱动程序异常

java - fireTableStructureChanged 后如何在 JTable 中维护用户指定的表行排序?

java - 如何使用 Java 按字母顺序对文件中的姓氏进行排序?

php - Windows Azure SDK for PHP 需要allow_url_fopen

php - 根据一次搜索查询两个表

php - 使用 PHP 检查 MySQL 中是否存在多行

java - 在主类中调用/测试排序方法

php - 合并多个具有相同结构的 mySQL 数据库

php - 在 Woocommerce 3 中访问订单项目保护数据