在论坛上获取某个类别的最新帖子

时间:2019-03-20 17:50:41

标签: mysql sql

我正试图进行一个SQL查询,该查询将从数据库中获取每个类别,并将它们与该类别中的最新帖子一起加入。

我使用MySQL

内容的层次结构是这样

类别>论坛>帖子

这是这些表的说明

类别

+-------+---------------------------+------+-----+---------+----------------+
| Field | Type                      | Null | Key | Default | Extra          |
+-------+---------------------------+------+-----+---------+----------------+
| id    | int(10) unsigned          | NO   | PRI | NULL    | auto_increment |
| title | varchar(100)              | NO   |     | NULL    |                |
| icon  | varchar(100)              | NO   |     | NULL    |                |
| color | set('red','green','pink') | NO   |     | NULL    |                |
+-------+---------------------------+------+-----+---------+----------------+

论坛

+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| title       | varchar(100)     | NO   |     | NULL    |                |
| subtitle    | varchar(100)     | NO   |     | NULL    |                |
| category_id | int(10) unsigned | NO   | MUL | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+

帖子

+------------+------------------+------+-----+------------------------+
| Field      | Type             | Null | Key | Default|  Extra        |
+------------+------------------+------+-----+--------+---------------+
| id         | int(10) unsigned | NO   | PRI | NULL   |auto_increment |
| title      | varchar(100)     | NO   |     | NULL   |               |
| content    | longtext         | NO   |     | NULL   |               |
| forum_id   | int(10) unsigned | NO   | MUL | NULL   |               |
| slug       | varchar(100)     | NO   | MUL | NULL   |               |
| created_at | timestamp        | NO   |     | TS     |               |
| updated_at | timestamp        | NO   |     | 0000   | on update CTP |
+------------+------------------+------+-----+------------------------+

因此,为了获得每个类别的最新帖子,我已经能够进行查询以实际获得我想要的东西,但是我认为这不是最好的方法,而是会更聪明地体会到这一点,想到了

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 |
+---+---------------------+-----------------+----------+----------------------------------+

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

这是数据库的转储 here

2 个答案:

答案 0 :(得分:1)

这绝对不是正确的方法。您正在将GROUP BYSELECT *一起使用,大多数数据库都不支持,甚至使用默认设置的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;

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

答案 1 :(得分:0)

这应该可以提供您想要的东西。

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