Sql整理出“病毒式”主题(最常阅读的新主题)

时间:2015-08-25 11:38:50

标签: php mysql phpbb

我有下面的代码可以正常工作。但是我想在这个函数中添加一个sql查询来排序“最常阅读/查看过的新主题”。

正如您所看到的,已经有一个查询来整理最常查看的主题 - “sql_views”,我想复制和更改该查询,以便它只在100个最新主题(id)中搜索。这可能是一个新的查询“sql_newview”或类似的。

我坚持这一点,但我想这对于一个查询大师来说非常简单,所以我希望在这里提供帮助!

例如:在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个最新主题中的“查看次数最多的主题”进行排序。

1 个答案:

答案 0 :(得分:0)

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