我正在尝试使用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(s)。所以我尝试加入了注释表并使用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
答案 0 :(得分: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