0

在我网站的主页上,我想显示论坛的最新帖子,但我不想两次显示相同的主题。如何修改下面的代码来做到这一点?

http://punbb.informer.com/wiki/punbb13/integration#recent_10_posts

基本上显示最新帖子,但每个论坛主题/线程仅显示一次。

4

3 回答 3

3

添加条件以仅保留帖子是主题中最后一个帖子的记录:

WHERE p.id = (
  SELECT pp.id 
  FROM posts AS pp
  WHERE pp.topic_id = t.id ORDER BY pp.posted DESC LIMIT 1
)
于 2010-02-06T18:11:15.360 回答
1

更改此行

'SELECT'    => 'p.id, p.message, t.subject',

'SELECT DISTINCT'   => 'p.id, p.message, t.subject',
于 2010-02-06T18:03:08.517 回答
1

如果您只希望每个主题有一个值,则可以按主题分组,然后从每个主题中选择最新的帖子。然后,您可以选择前 10 个主题。

我将用 SQL 编写它,您可以将其转换为 PHP:

SELECT p.id, p.message, o.subject
FROM
((SELECT t.id
  FROM posts AS p LEFT JOIN topics AS t ON p.topic_id = t.id
  GROUP BY t.id
  HAVING p.posted = MAX(p.posted) ) ids LEFT JOIN topics AS t ON ids.id = t.id) o
                                        LEFT JOIN posts AS p ON o.id = posts.topic_id
ORDER BY p.posted DESC
LIMIT '0,10'
于 2010-02-06T18:15:05.460 回答