Select 具有最新 post、用户和主题信息的所有类别

Select all categories with latest post, user, and topic information

我正在为一个网络项目开发自定义论坛。我有带有 id 的类别,带有 id 和类别的主题,以及带有 id 和主题以及用户 id 的 posts。我想要做的是显示类别列表,其中包含来自类别 table 的数据以及来自 posts table 的最新 post 的数据类别、该 post 的相关用户的数据,以及与最新 post.

相关的主题的一些数据

我一直在用头撞墙试图弄清楚查询,但我对复杂的 mysql 查询没有足够的理解,不知道在这里使用什么模式或技术.这是我目前所拥有的:

SELECT u1.*, fp1.*, ft1.*, fc1.* from forum_posts AS fp1
LEFT JOIN users AS u1 ON u1.id = fp1.post_by
LEFT JOIN forum_topics AS ft1 on ft1.id = fp1.post_topic
LEFT JOIN forum_categories AS fc1 on fc1.id = ft1.topic_cat
GROUP BY fc1.id
ORDER BY fp1.id ASC;

但这不是我要找的 return 结果。问题在于尝试获取每个类别的最新 post 以及该 post 的关联主题。

这是每个 table:

的数据库结构

forum_categories

+-----------------+---------------------+------+-----+---------+----------------+
| Field           | Type                | Null | Key | Default | Extra          |
+-----------------+---------------------+------+-----+---------+----------------+
| id              | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| cat_name        | varchar(255)        | NO   | UNI | NULL    |                |
| cat_description | varchar(500)        | NO   |     | NULL    |                |
| cat_views       | bigint(20) unsigned | YES  |     | 0       |                |
| status          | tinyint(1)          | NO   |     | 1       |                |
+-----------------+---------------------+------+-----+---------+----------------+

forum_topics

+---------------+---------------------+------+-----+---------+----------------+
| Field         | Type                | Null | Key | Default | Extra          |
+---------------+---------------------+------+-----+---------+----------------+
| id            | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| topic_subject | varchar(255)        | NO   |     | NULL    |                |
| topic_date    | datetime            | NO   |     | NULL    |                |
| topic_cat     | bigint(20) unsigned | NO   | MUL | NULL    |                |
| topic_by      | bigint(20) unsigned | NO   | MUL | NULL    |                |
| topic_views   | bigint(20) unsigned | YES  |     | 0       |                |
+---------------+---------------------+------+-----+---------+----------------+

forum_posts

+--------------+---------------------+------+-----+---------+----------------+
| Field        | Type                | Null | Key | Default | Extra          |
+--------------+---------------------+------+-----+---------+----------------+
| id           | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| post_content | text                | NO   |     | NULL    |                |
| post_date    | datetime            | NO   |     | NULL    |                |
| post_topic   | bigint(20) unsigned | NO   | MUL | NULL    |                |
| post_by      | bigint(20) unsigned | NO   | MUL | NULL    |                |
+--------------+---------------------+------+-----+---------+----------------+

用户

+-----------+---------------------+------+-----+---------+----------------+
| Field     | Type                | Null | Key | Default | Extra          |
+-----------+---------------------+------+-----+---------+----------------+
| id        | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| user_type | varchar(50)         | YES  |     | NULL    |                |
| email     | varchar(255)        | NO   | UNI | NULL    |                |
| username  | varchar(255)        | YES  | UNI | NULL    |                |
| password  | char(60)            | NO   |     | NULL    |                |
| image     | text                | YES  |     | NULL    |                |
| status    | tinyint(1)          | NO   |     | 1       |                |
+-----------+---------------------+------+-----+---------+----------------+

这是我要实现的输出图像。 "Categories" 显示来自 forum_categories table 的数据,"Recent" 下是用户,post 和主题数据为最新的 post:

请帮帮我。谢谢。

虽然将所有表连接在一起以计算出每个 post 的主题、类别和用户非常简单,但您还需要一个额外的步骤 - 您需要连接到检索 post 的子查询=11=] 每个类别。

您可以通过以下一种方式做到这一点:

select fc.cat_name, fc.cat_description, fc.cat_views, u.username, fp.post_date, ft.topic_subject
  from forum_categories fc
    inner join forum_topics ft
      on fc.id = ft.topic_cat
    inner join forum_posts fp
      on fp.post_topic = ft.id
    inner join users u
      on fp.post_by = u.id
    inner join (
      select topic_cat, max(fp.id) most_recent_post
        from forum_topics ft
          inner join forum_posts fp
            on fp.post_topic = ft.id
      group by topic_cat
    ) q
      on q.topic_cat = ft.topic_cat
        and fp.id = q.most_recent_post;

这里有一个演示可以玩:http://sqlfiddle.com/#!9/3736b/1