doulongti5932 2014-04-20 22:38
浏览 21
已采纳

显示最受欢迎/浏览过的文章 - 按时间划分

i have a news website

On the home page i want to list the 4 most viewed articles in the last 7 days

i have a table, Articles: id, title, body, publishTime, viewCount...

i cant just display the top 4 in order of view count, as the longer it has been out, the generally more views it will have. so i need to divide views by hours displayed

so my query needs to get all articles in last 168 hours, divide each view count by hours shown, then just show top 4

is it possible to do this with just commands, or do i need to create some kind of virtual table?

(using php & mysql)

thanks

  • 写回答

1条回答 默认 最新

  • dongyihang3575 2014-04-20 23:17
    关注

    Per our discussion in the comments, it seems that you're not after "the 4 most viewed articles in the last 7 days" but rather "the 4 articles with the highest average view rate that have been published in the last 7 days". This can be achieved as follows:

    SELECT   *
    FROM     Articles
    WHERE    publishTime > CURRENT_TIME - INTERVAL 7 DAY
    ORDER BY viewCount / (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(publishTime)) DESC
    LIMIT    4
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用