我目前正在为我的学校项目构建自己的论坛,此查询列出了所有论坛类别,但是我有问题检查是否有用户未读取的主题/主题,然后将论坛类别标记为未读消息。
它在子查询中的'字段列表'中显示“未知列'forum_category.id'”我已经检查了子查询的几个例子,从我看到的我应该能够访问 forum_category.id 并在子查询中使用它?我不知道我在这一点上做错了什么...
非常感谢帮助!
SELECT forum_category.id
, root.name AS root_name
, subcat.name AS subcat_name
, subcat.id AS subcat_id
, subcat.description AS subcat_description
, subcat.safe_url AS subcat_safe_url
, topics.topic_id
, topics.topic_safe_url
, topics.topic_title
, topics.last_post_time
, topics.topic_last_poster_name
, topics.topic_last_poster_id
, (
SELECT
posts_read.last_read_time
FROM
forum_topics a
LEFT JOIN
forum_posts_read AS posts_read ON
posts_read.last_read_time > a.last_post_time
AND posts_read.last_read_time > 1321004546
AND posts_read.topic_id = a.topic_id
AND posts_read.user_id = 1
AND a.forum_id = forum_category.id
LIMIT 1) AS last_read_time
FROM forum_category AS root
LEFT JOIN
forum_category AS subcat ON subcat.parent_id = root.id
LEFT JOIN
forum_topics AS topics ON topics.forum_id = subcat.id
LEFT JOIN
forum_topics AS t2 ON t2.forum_id = subcat.id AND t2.last_post_time > topics.last_post_time
WHERE
root.parent_id = 0 AND t2.forum_id IS NULL
ORDER BY
root_name, subcat_name
我知道尝试了这个,但它只检查每个类别中的第一个主题/主题...
SELECT root.name AS root_name
, subcat.name AS subcat_name
, subcat.id AS subcat_id
, subcat.description AS subcat_description
, subcat.safe_url AS subcat_safe_url
, topics.topic_id
, topics.topic_safe_url
, topics.topic_title
, topics.last_post_time
, topics.topic_last_poster_name
, topics.topic_last_poster_id
, posts_read.last_read_time
FROM forum_category AS root
LEFT JOIN
forum_category AS subcat ON subcat.parent_id = root.id
LEFT JOIN
forum_topics AS topics ON topics.forum_id = subcat.id
LEFT JOIN
forum_topics AS t2 ON t2.forum_id = subcat.id AND t2.last_post_time > topics.last_post_time
LEFT JOIN
forum_posts_read AS posts_read ON
posts_read.last_read_time > topics.last_post_time
AND posts_read.last_read_time > ?
AND posts_read.topic_id = topics.topic_id
AND posts_read.user_id = ?
AND topics.forum_id = subcat.id
WHERE
root.parent_id = 0 AND t2.forum_id IS NULL
ORDER BY
root_name, subcat_name
谁知道这会很难...我的意思是我想做的事情很简单:(
答案 0 :(得分:0)
表。我相信你的问题在于表的别名。你告诉引擎不要使用forum_category而是使用ROOT或subcat。所以SQL不知道一个forum_Category(它没有在“FROM”中看到该表。外部select也存在同样的问题。也将它改为root。
SELECT root.id
, root.name AS root_name
, subcat.name AS subcat_name
, subcat.id AS subcat_id
, subcat.description AS subcat_description
, subcat.safe_url AS subcat_safe_url
, topics.topic_id
, topics.topic_safe_url
, topics.topic_title
, topics.last_post_time
, topics.topic_last_poster_name
, topics.topic_last_poster_id
, (
SELECT
posts_read.last_read_time
FROM
forum_topics a
LEFT JOIN
forum_posts_read AS posts_read ON
posts_read.last_read_time > a.last_post_time
AND posts_read.last_read_time > 1321004546
AND posts_read.topic_id = a.topic_id
AND posts_read.user_id = 1
AND a.forum_id = Root.id
LIMIT 1) AS last_read_time
FROM forum_category AS root
LEFT JOIN
forum_category AS subcat ON subcat.parent_id = root.id
LEFT JOIN
forum_topics AS topics ON topics.forum_id = subcat.id
LEFT JOIN
forum_topics AS t2 ON t2.forum_id = subcat.id AND t2.last_post_time > topics.last_post_time
WHERE
root.parent_id = 0 AND t2.forum_id IS NULL
ORDER BY
root_name, subcat_name