繁体   English   中英

用子查询结果联接表

[英]Join table with a subquery result

我正在尝试使用PHP和Mysql创建论坛。 我有这两个表:

论坛主题

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| project     | int(11)      | YES  | MUL | NULL    |                |
| title       | varchar(255) | NO   |     | NULL    |                |
| description | varchar(255) | NO   |     | NULL    |                |
| created     | datetime     | YES  |     | NULL    |                |
| is_locked   | tinyint(1)   | NO   |     | 0       |                |
| ForumTopic  | int(11)      | YES  | MUL | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

论坛评论

+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| usr        | int(11)      | YES  | MUL | NULL    |                |
| content    | varchar(255) | NO   |     | NULL    |                |
| created    | datetime     | YES  |     | NULL    |                |
| is_deleted | tinyint(1)   | NO   |     | NULL    |                |
| ForumTopic | int(11)      | YES  | MUL | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

有一些根主题,其中包含子主题。 子主题只能包含评论。 如果ForumTopic(在Forum_topic表内部,是的,我知道命名错误)列为null,则为Root。 否则,它包含根论坛主题的ID。

我需要一个查询,该查询输出每个根目录类别中的主题数(类别=根目录主题),每个根目录类别的评论数(即所有子主题的评论已汇总)以及最后,该类别的最后一个评论的整行未被删除(is_deleted = 0)。 我到目前为止所拥有的是:

SELECT ID as topicId
     , (select count(*) 
          FROM `forum_topic` 
         where ForumTopic = topicId) as TopicCount
     , (SELECT count(*) 
          FROM `forum_comment` 
         where ForumTopic in (SELECT ID 
                                FROM `forum_topic` 
                               where ForumTopic = topicId) 
         ORDER 
            BY created desc) as postNumber
     , (SELECT ID FROM `forum_comment` 
         where ForumTopic in (SELECT ID 
                                FROM `forum_topic` 
                               where ForumTopic = topicId) 
         ORDER 
            BY created desc limit 1) as lastPostId 
  FROM `forum_topic` 
 where ForumTopic IS NULL 
   and project is null

具有以下输出:

+---------+------------+------------+------------+
| topicId | TopicCount | postNumber | lastPostId |
+---------+------------+------------+------------+
|       1 |          5 |         15 |         43 |
|       2 |          1 |          2 |         13 |
|       3 |          8 |          2 |         30 |
|       4 |          0 |          0 |       NULL |
+---------+------------+------------+------------+

我使用子查询来获得此结果,这与我所需要的非常接近。 但是,我真正需要的是具有该ID的行,而不是lastPostId。 因此,我尝试加入注释表,并使用lastPostId作为要加入的列,但它不起作用。 这是我的加入尝试

SELECT forum_topic.ID as topicId, (select count(*) FROM `forum_topic` where ForumTopic = topicId) as TopicCount,(SELECT count(*) FROM `forum_comment` where ForumTopic in (SELECT ID FROM `forum_topic` where ForumTopic = topicId) ORDER BY created desc) as postNumber,(SELECT ID FROM `forum_comment` where ForumTopic in (SELECT ID FROM `forum_topic` where ForumTopic = topicId) ORDER BY created desc limit 1) as lastPostId 
FROM `forum_topic`
join forum_comment on lastPostId = forum_comment.id
where forum_topic.ForumTopic IS NULL and forum_topic.project is null

这将返回1054 - Unknown column 'lastPostId' in 'on clause'

我如何获得最后的评论行?

编辑:示例数据可以在这里找到: http : //sqlfiddle.com/#!9/b65cfa/1

并且预期结果应该是 预期结果

您收到此错误,是因为您加入了查询而不是没有别名的表

因此,您需要使用这样的别名来包装查询

    (SELECT forum_topic.ID as topicId
        , (select count(*) FROM `forum_topic` where ForumTopic = topicId) as TopicCount
        ,(SELECT count(*) FROM `forum_comment` where ForumTopic in (SELECT ID FROM `forum_topic` where ForumTopic = topicId) ORDER BY created desc) as postNumber
        ,(SELECT ID FROM `forum_comment` where ForumTopic in (SELECT ID FROM `forum_topic` where ForumTopic = topicId) ORDER BY created desc limit 1) as lastPostId 
        FROM `forum_topic` where forum_topic.ForumTopic IS NULL and forum_topic.project is null) A

现在,您从查询中获得了新表A,并在A和forum_comment之间执行联接

    Select A.topicId
        ,A.TopicCount
        ,A.postNumber
        ,A.lastPostId
            from (SELECT forum_topic.ID as topicId
                    , (select count(*) FROM `forum_topic` where ForumTopic = topicId) as TopicCount
                    ,(SELECT count(*) FROM `forum_comment` where ForumTopic in (SELECT ID FROM `forum_topic` where ForumTopic = topicId) ORDER BY created desc) as postNumber
                    ,(SELECT ID FROM `forum_comment` where ForumTopic in (SELECT ID FROM `forum_topic` where ForumTopic = topicId) ORDER BY created desc limit 1) as lastPostId 
                    FROM `forum_topic` where forum_topic.ForumTopic IS NULL and forum_topic.project is null) A

                join forum_comment on A.lastPostId = forum_comment.id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM