SQL-如何提取不重复的论坛主题?

发布于 2024-08-20 16:03:45 字数 276 浏览 10 评论 0原文

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

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

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

On the home page of my website I want to display the latest posts to the forum however I don't want to show the same topic twice. How can I modify the code below to do this?

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

Basically show the latest posts, but only once for each forum topic/thread.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猛虎独行 2024-08-27 16:03:45

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

WHERE p.id = (
  SELECT pp.id 
  FROM posts AS pp
  WHERE pp.topic_id = t.id ORDER BY pp.posted DESC LIMIT 1
)

Add a condition to keep only records where the post is the last post in the topic:

WHERE p.id = (
  SELECT pp.id 
  FROM posts AS pp
  WHERE pp.topic_id = t.id ORDER BY pp.posted DESC LIMIT 1
)
謌踐踏愛綪 2024-08-27 16:03:45

如果您只需要每个主题一个值,您可以按主题分组,然后从每个主题中选择最新的帖子。然后,您可以选择前 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'

If you want only one value per topic, you could group by topic, and from each topic select the most recent post. Then, you could choose the top 10 topics.

I'll write it in SQL, and you can translate that to 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'
你是暖光i 2024-08-27 16:03:45

将此行更改

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

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

change this line

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

to

'SELECT DISTINCT'   => 'p.id, p.message, t.subject',
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文