Mysql 在论坛上获取类别中的最新帖子

Mysql 在论坛上获取类别中的最新帖子,mysql,sql,Mysql,Sql,我正在尝试进行一个SQL查询,该查询将从数据库中获取每个类别,并将它们与该类别中的最新帖子连接起来 我使用MySQL 内容的层次结构也是如此 类别>论坛>帖子 这是对这些表格的描述 类别 论坛 职位 因此,为了从每个类别中获得最新的帖子,我已经能够提出一个查询,实际上得到了我想要的东西,但我认为这不是最好的方式,我会欣赏一个更聪明的方式,这就是我提出的 SELECT * FROM (SELECT `categories`.`id`, `posts`.`title

我正在尝试进行一个SQL查询,该查询将从数据库中获取每个类别,并将它们与该类别中的最新帖子连接起来

我使用MySQL

内容的层次结构也是如此

类别>论坛>帖子

这是对这些表格的描述

类别

论坛

职位

因此,为了从每个类别中获得最新的帖子,我已经能够提出一个查询,实际上得到了我想要的东西,但我认为这不是最好的方式,我会欣赏一个更聪明的方式,这就是我提出的

SELECT *
FROM   (SELECT `categories`.`id`,
               `posts`.`title`      AS post_title,
               `categories`.`title` AS cat_title,
               `posts`.`created_at` AS created
        FROM   `categories`
               JOIN `forums`
                 ON `forums`.`category_id` = `categories`.`id`
               JOIN `posts`
                 ON `posts`.`forum_id` = `forums`.`id`
        ORDER  BY `created` DESC
        LIMIT  18446744073709551615) AS sub
GROUP  BY `id`  
============================编辑====================

样本类别表

论坛表格样本

样本邮政表格

因此,对于每个类别,我需要基于创建日期的最新帖子


这是数据库的转储

这绝对不是正确的方法。您正在使用GROUP BY with SELECT*,大多数数据库都不支持它,使用默认设置的MySQL的最新版本也不支持它

相反:

SELECT c.id, p.title as post_title, c.title as cat_title,
       p.created_at AS created
FROM categories c JOIN
     forums f
     ON f.category_id = c.id JOIN
     posts p
     ON p.forum_id = f.id
WHERE p.created_at = (SELECT MAX(p2.created_at)
                      FROM posts p2 JOIN
                           forums f2
                           ON p2.forum_id = f2.id
                      WHERE f2.category_id = f.category_id
                     )
ORDER  BY created_at DESC;

子查询正在计算在日期为给定类别的帖子创建的最大值。

这应该是您想要的

SELECT *
FROM (
  SELECT c.id, p.title AS post_title, c.title AS cat_title, p.created_at AS created
      , ROW_NUMBER() OVER(PARTITION BY c.id ORDER BY p.created_at) AS rn
  FROM categories c
  JOIN forums f ON f.category_id = c.id
  JOIN posts p ON p.forum_id = f.id
) a
WHERE rn = 1

示例数据和所需的输出将非常有用。好的,我将在下面添加示例。输出根据创建日期从每个类别获取最新帖子。我将在bitadded示例数据中添加示例数据。您需要列出DBMS。SQL是标准的,而不是语言本身。你在使用Oracle、MySQL等吗?MySQL说:文档1064-你的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以了解在第行中使用“按c.id顺序按p.created_at AS rn划分c类连接”的正确语法4@olayemii如果您能解释子查询中的WHERE子句,我们将不胜感激,其他的我都懂
SELECT *
FROM   (SELECT `categories`.`id`,
               `posts`.`title`      AS post_title,
               `categories`.`title` AS cat_title,
               `posts`.`created_at` AS created
        FROM   `categories`
               JOIN `forums`
                 ON `forums`.`category_id` = `categories`.`id`
               JOIN `posts`
                 ON `posts`.`forum_id` = `forums`.`id`
        ORDER  BY `created` DESC
        LIMIT  18446744073709551615) AS sub
GROUP  BY `id`  
+------------------------+-----------+----------+
| id| title              | icon      | color    |
+------------------------+-----------+----------+
| 1 | General Forums     | fa-pencil | red      |
| 2 | Help & Disscussion | fa-person | blue     | 
+---+--------------------+--------+------+------+
+------------------------------+----------------------+--------------+
| id| title                    | subtitle             | category_id  |
+------------------------------+----------------------+--------------+
| 1 | Software Development     | About software dev   | 1            |
+---|--------------------------|----------------------|--------------|
| 2 | Graphics Design          | About graphics des   | 2            |
+---+--------------------------+-----------+----------+--------------|
+---+---------------------+----------------------------+----------------------------------+
| id| title               | content         | forum_id | slug       | created_at          |
+-------------------------+----------------------------+----------------------------------+
| 1 | Memoizing in JS     | Lorem Ipsum     | 1        |  memo-js   | 2019-03-21 00:45:54 |
+---+---------------------+----------------------------+------------+---------------------+
| 2 | Using headers in C# | Lorem Ipsum     | 1        | using-he   | 2019-03-20 00:45:54 |
+---+---------------------+-----------------+----------+----------------------------------+
SELECT c.id, p.title as post_title, c.title as cat_title,
       p.created_at AS created
FROM categories c JOIN
     forums f
     ON f.category_id = c.id JOIN
     posts p
     ON p.forum_id = f.id
WHERE p.created_at = (SELECT MAX(p2.created_at)
                      FROM posts p2 JOIN
                           forums f2
                           ON p2.forum_id = f2.id
                      WHERE f2.category_id = f.category_id
                     )
ORDER  BY created_at DESC;
SELECT *
FROM (
  SELECT c.id, p.title AS post_title, c.title AS cat_title, p.created_at AS created
      , ROW_NUMBER() OVER(PARTITION BY c.id ORDER BY p.created_at) AS rn
  FROM categories c
  JOIN forums f ON f.category_id = c.id
  JOIN posts p ON p.forum_id = f.id
) a
WHERE rn = 1