【问题标题】:Given two tables, select all data from one table and only the most recent from the other给定两个表,从一个表中选择所有数据,并且只从另一个表中选择最近的数据
【发布时间】:2016-12-05 17:27:00
【问题描述】:

我正在尝试构建一个论坛网站,该网站使用 PHP 和 MySQL 数据库来存储其类别和主题内容。在主页上,我想要一个表格,显示所有类别的列表以及每个类别中最近发布的主题。

我想编写一个查询,它返回类别表中的所有类别以及主题表中每个类别的最新主题。

我的表格如下所示:

分类

+--------+-------------+--------------------------+
| cat_id |  cat_name   |     cat_description      |
+--------+-------------+--------------------------+
|      1 | Automobiles | *Tim Taylor manly grunt* |
|      2 | English     | How to English           |
|      3 | Gardening   | Lawn and Order           |
+--------+-------------+--------------------------+

主题

+----------+-----------------------+------------------------+-----------+----------+
| topic_id |      topic_name       |     topic_content      | topic_cat | topic_by |
+----------+-----------------------+------------------------+-----------+----------+
|        1 | '67 Chevy Question    | Weird knocking noise?  |         1 |        1 |
|        2 | You're vs Your        | What's the difference? |         2 |        3 |
|        3 | Jumper cables?        | The proper hookup      |         1 |        2 |
|        4 | '03 Pathfinder        | Next newest model?     |         1 |        1 |
|        5 | There, Their, They're | Know the difference    |         2 |        4 |
+----------+-----------------------+------------------------+-----------+----------+

我在技巧 #3 下的 https://stackoverflow.com/a/12586263/7249891 上找到了相关答案,但经过几个小时的摆弄,我无法将其归结为适合我的查询。

我的问题是,如何调整我的原始查询

SELECT c.cat_name AS Category, t.topic_cat AS Recent Topic 
FROM categories c
JOIN topics t
WHERE c.cat_id = t.topic_cat

所以它返回数据库中的所有类别,但在类似于此的结果中只返回每个类别中的最新主题

+-------------+-----------------------+
|  Category   |     Recent Topic      |
+-------------+-----------------------+
| Automobiles | '03 Pathfinder        |
| English     | There, Their, They're |
| Gardening   | NULL                  |
+-------------+-----------------------+

说明: 在这个论坛中,管理员创建了几个类别,任何用户都可以在其中发布主题。

在主题中,主题主题是用户提出的问题,主题内容是关于该问题的附加信息。

Cat_id 和 topic_id 都是自增主键。

Topic_subject 是引用 cat_id 的外键。

假设由于主键行为,主题表中的最新主题是具有最高 topic_id 编号的主题。此表中还有一个日期字段(我意识到最后一分钟我忘了包括在这里)。

还有两个我没有在这里列出的表:用户表和回复表。 topic_by 是引用 users 表的外键。

如果某个类别(我上面的示例中的园艺类别)中没有主题,我们将假设程序的 PHP 部分将使列表中的该部分显示为“(无)”。

【问题讨论】:

    标签: mysql


    【解决方案1】:

    首先找到每个类别的最新帖子:

    select topic_cat, max(topic_id) as latest_topic
    from topics group by topic_cat
    

    然后将其添加到您的加入条件中:

    SELECT  c.cat_name AS Category, t.topic_name AS Recent_Topic 
    FROM categories c
    left JOIN topics t on c.cat_id = t.topic_cat 
    left join (select topic_cat, max(topic_id) as latest_topic
            from topics group by topic_cat) as latest_topics 
            on latest_topics.topic_cat = c.cat_id
            and latest_topics.latest_topic = t.topic_id 
    where latest_topics.topic_cat is not null or t.topic_cat is null;
    

    【讨论】:

    • 谢谢,这给了我想要的结果。我一直在尝试分解查询以完全了解发生了什么,但我对 SELECT 中的 SELECT 的概念仍然很陌生。
    • 你试过只运行select topic_cat, max(topic_id) as latest_topic from topics group by topic_cat吗?将这些结果视为另一个表,然后您将加入其他“正常”表。
    【解决方案2】:

    首先从topics 表中选择具有按topic_cat 分组的最大id 的行,然后与类别表进行左连接。

    查询

    select a.`cat_name`, 
           b.`topic_name` as `Recent Topic`
    from `categories` a
    left join(
      select t1.`topic_id`, 
             t1.`topic_name`, 
             t1.`topic_content`, 
             t1.`topic_cat`, 
             t1.`topic_by` from 
      (
        select `topic_id`, 
               `topic_name`, 
               `topic_content`, 
               `topic_cat`, 
               `topic_by`,
        (
            case `topic_cat` when @curA 
            then @curRow := @curRow + 1 
            else @curRow := 1 and @curA := `topic_cat` end 
        ) as `rn` 
        from `topics` t, 
        (select @curRow := 0, @curA := '') r 
        order by `topic_cat`, `topic_id` desc 
        )t1 
        where t1.`rn` = 1
    )b
    on a.`cat_id` = b.`topic_cat`;
    

    SQL Fiddle Demo

    【讨论】:

      猜你喜欢
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 2014-12-04
      • 2017-11-13
      • 2014-02-17
      • 1970-01-01
      • 2011-03-30
      • 2013-07-17
      相关资源
      最近更新 更多