我有下面的代碼工作正常。但是我想爲這個函數添加一個sql查詢,以排除「讀取/查看次數最多的新主題」。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個最新主題中對「最受歡迎的主題」進行排序。
您可以爲表添加create table語句嗎? –
嗨,是它的標準phpbb表:https://wiki.phpbb.com/Table.phpbb_topics和https://wiki.phpbb.com/Table.phpbb_posts – stavasknall