Mysql 子查询中的未知列为什么?

Mysql 子查询中的未知列为什么?,mysql,subquery,Mysql,Subquery,我目前正在为我的学校项目构建一个论坛,此查询列出了所有论坛类别,但我在检查是否有用户未阅读的主题/线程时遇到问题,然后将论坛类别标记为有未读消息 它在子查询中显示“字段列表”中的“forum_category.id”未知列。。。我已经检查了几个关于子查询的示例,根据我所看到的,我应该能够访问论坛\ u category.id,并在子查询中使用它?我不知道我现在做错了什么 非常感谢您的帮助 SELECT forum_category.id , root.name AS root_n

我目前正在为我的学校项目构建一个论坛,此查询列出了所有论坛类别,但我在检查是否有用户未阅读的主题/线程时遇到问题,然后将论坛类别标记为有未读消息

它在子查询中显示“字段列表”中的“forum_category.id”未知列。。。我已经检查了几个关于子查询的示例,根据我所看到的,我应该能够访问论坛\ u 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

谁想到会这么难。。。我的意思是,我想做的事情相当简单:(

表。我相信您的问题在于表的别名。您告诉引擎不要使用forum\u类别,而是使用ROOT或subcat。因此SQL不知道forum\u类别(它在“FROM”中看不到该表)。外部选择中也存在相同的问题。请将其更改为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

这是因为您将forum_类别别名为ROOT try
和a.forum_id=ROOT.id
Hello xQbert,我也这么认为,但它并没有改变on子句中未知列“ROOT.id”的错误,我真的很担心:)我很感谢您的帮助!我的另一个想法是再添加一个左连接,去掉子查询,并按其他所有内容获取上次读取时间分组的最小值/最大值;但由于看不到数据/表,所以我很难做到这一点。