1

我试图显示最近两天最流行的项目,但这个视图是让两天前发生的项目进入。

它是用来查找最近两天最受欢迎的(可能是 20-30 个项目)并用随机项目填充剩余的项目(需要始终在视图中显示 1000 个项目)

我怎样才能解决这个问题?

谢谢

CREATE 
    ALGORITHM = UNDEFINED 
    DEFINER = `XX`@`XX` 
    SQL SECURITY DEFINER
VIEW `trending` AS
    select 
        `question`.`name` AS `name`,
        `question`.`questionUrl` AS `questionUrl`,
        `question`.`miRating` AS `miRating`,
        `question`.`imageUrl` AS `imageUrl`,
        `question`.`miThumbnail` AS `miThumbnail`,
        `question`.`foundOn` AS `foundOn`,
        `question`.`myId` AS `myId`
    from
        (`question`
        join `feed` ON ((`question`.`myId` = `feed`.`question_id`)))
    group by `question`.`name`
    order by (`feed`.`timeStamp` >= (now() - interval 1 day)) desc , 
    (`feed`.`question_id` is not null) desc , 
    (((`question`.`likesCount` * 0.8) + (`question`.`commentsCount` * 0.6)) + ((`question`.`sharesCount` * 1) / 2.4)) desc
    limit 0 , 1000
4

1 回答 1

1

问题是您按 分组question_name,但查询中有很多其他列,在select和中都有order by。MySQL 为这些选择任意值。解决此问题的一种方法是仅使用order by子句中的最大时间条件:

select q.`name` AS `name`, q.`questionUrl` AS `questionUrl`, q.`miRating` AS `miRating`,
       q.`imageUrl` AS `imageUrl`, q.`miThumbnail` AS `miThumbnail`,
       q.`foundOn` AS `foundOn`, q.`myId` AS `myId`
from `question` q join
     `feed` f
      ON q.`myId` = f.`question_id`
group by q.`name`
order by (max(f.`timeStamp`) >= (now() - interval 1 day)) desc , 
         (f.`question_id` is not null) desc , 
         (((q.`likesCount` * 0.8) + (q.`commentsCount` * 0.6)) + ((q.`sharesCount` * 1) / 2.4)) desc
limit 0 , 1000
于 2013-09-15T16:10:33.633 回答