Sql sort out "病毒"主题(最常阅读的新主题)


Sql sort out "viral" topics (new topics that are most read)

下面的代码可以正常工作。但是我想在这个函数中添加一个sql查询,以整理"阅读/查看最多的新主题"。

正如您所看到的,已经有一个查询用于排序查看最多的主题-"sql_views",我想复制并更改该查询,以便它只在100个最新主题(id)中搜索。这可以是一个新的查询"sql_newview"或类似的。

我被这个困住了,但我想这对于一个查询大师来说很简单,所以我希望在这里得到帮助!

E。g:找到TOPICS_TABLE中最近的100个条目,并使用t.topic_views DESC对它们进行排序。

$sql_main = "SELECT  t.*, MAX(p.post_id) AS last_post
                FROM " . POSTS_TABLE . " AS p, " . TOPICS_TABLE . " AS t
                WHERE {available_forums_in_set}
                    AND p.topic_id = t.topic_id AND p.forum_id = t.forum_id
                GROUP BY p.topic_id";
$sql_tt_avails = array(
    'sql_topics'    => array(
        'sql'   =>  "{$sql_main}
                    ORDER BY t.topic_time DESC",
        'field' =>  't.forum_id',
        ),
    'sql_replies'   => array(
        'sql'   =>  "{$sql_main}
                    ORDER BY t.topic_replies DESC, t.topic_time DESC",
        'field' =>  't.forum_id',
        ),
    'sql_views'     => array(
        'sql'   =>  "{$sql_main}
                    ORDER BY t.topic_views DESC, t.topic_time DESC",
        'field' =>  't.forum_id',
        ),
    'sql_posts'     => array(
        'sql'   =>  "{$sql_main}
                    ORDER BY t.topic_last_post_time DESC",
        'field' =>  't.forum_id',
        ),
);

编辑:这里是完整的(有效的)查询,首先是"查看最多/阅读最多的主题"

SELECT t.*, MAX(p.post_id) AS last_post FROM phpbb_posts AS p, phpbb_topics AS t WHERE t.forum_id IN (1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 24, 25, 26, 27, 28, 29, 30, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 106, 107, 108, 109, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125) AND p.topic_id = t.topic_id AND p.forum_id = t.forum_id GROUP BY p.topic_id ORDER BY t.topic_views DESC, t.topic_time DESC LIMIT 5

下面是"最新主题"的查询

SELECT t.*, MAX(p.post_id) AS last_post FROM phpbb_posts AS p, phpbb_topics AS t WHERE t.forum_id IN (1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 24, 25, 26, 27, 28, 29, 30, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 106, 107, 108, 109, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125) AND p.topic_id = t.topic_id AND p.forum_id = t.forum_id GROUP BY p.topic_id ORDER BY t.topic_time DESC LIMIT 5

我已经尝试了几次编辑,但不能让"sql_views"从100个最新主题中排序"最多查看的主题"

SQL有一个限制子句,例如SELECT * FROM {table} WHERE {condition} LIMIT 100