0

我有喜欢/不喜欢游戏的表格:id、game_id、类型(喜欢/不喜欢)、时间

表格示例:图片链接

这段代码给了我上周喜欢的游戏,按他们的喜欢计数排序:

$limit = 10;
$time = _time() - 60*60*24*7;
$games_id = array();
$games_id_query = $this->db->execQuery("SELECT `game_id`, count(*) as `likes_count` FROM `likes` WHERE `type` = 'like' AND `time` > '{$time}' group by `game_id` order by `likes_count` DESC LIMIT {$limit}");
$games_id_num = $games_id_query->num_rows;
if($games_id_num > 0) {
    while($row = $games_id_query->fetch_object()) {
        unset($row->likes_count);
        $games_id[] = (array) $row;
    }
}

我想进行此查询,仅按上周获得的喜欢订购。

示例:如果游戏 X 有 5 个赞,游戏 Y 有 6 个赞,但是游戏 X 在本周获得了 3 个赞,而游戏 Y 在本周获得了 2 个赞 - 游戏 X 将成为第一,游戏 Y 将成为第二。

非常感谢你 !

4

2 回答 2

0

您需要使用一个案例:

SELECT `game_id`, COUNT(*) AS `likes_count` 
FROM `likes` 
WHERE `TYPE` = 'like' AND `TIME` > '{$time}' 
GROUP BY `game_id` 
ORDER BY COUNT(CASE WHEN `TIME` >= CURRENT_TIMESTAMP - INTERVAL '7' DAY END) DESC 
LIMIT {$limit}

您的 DBMS 似乎是 MySQL,afaik 它支持 INTERVAL 算术和 ORDER 计算。

于 2013-08-17T15:11:46.757 回答
0

添加一周的列,并按该列按降序排列。

像这样的东西

SELECT game_id
       ,DATEPART(wk, likeDate)
       ,count(*) as `likes_count`
FROM   likes 
WHERE  typ = 'like' 
AND    time > '{$time}' 
group by
       game_id
       ,DATEPART(wk, likeDate)
order by 
       2 DESC 
LIMIT  {$limit}
于 2013-08-17T14:43:45.870 回答