dongliqian6245 2018-03-24 16:32
浏览 43
已采纳

在过去10分钟内按文章视图从高到低排序

I have a list of article links on a page that I get from mysql and I would like to sort them in a way that the most viewed articles in the past 10 minutes display first at the top of the other articles. Currently what I am trying is when someone clicks an article a new row is inserted into the table "views" with the columns id, timestamp and article_id. I have another table called articles which has all the article data and has the column article_id as well which matches any articles in the views table.

Basically, when an article is clicked it inserts rows into the views table like this.

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

When results are displayed on the page i'd like to show the most clicked first as I said above. In this example 4 articles out of the 15 displayed have views/clicks so there is multiple rows of those 4 articles in the views table which need to be counted and displayed first from highest to lowest and then have the rest of the normal article results with no clicks show as usual. Something like this if it makes sense.

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

I get quite a lot of traffic on the site that I am trying to do this on so I am trying to figure out the best way to execute the query and also try not to take a significant performance hit if possible.

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. I have been trying to come up with a query to do this but it never ends up working the way I have described.

Does anyone have any ideas or suggestions? Thanks.

EDIT:

Articles table:
id, article_id, title, description

Views table:
id, timestamp, article_id
  • 写回答

2条回答 默认 最新

  • dongren4758 2018-03-24 16:45
    关注

    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.

    Your query needs to be something like these queries below.

    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
    

    or

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 看图片)删除这个自动化录屏脚本就一直报错找不到脚本文件,如何解决?(相关搜索:bat文件)
  • ¥750 关于一道数论方面的问题,求解答!(关键词-数学方法)
  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集