简体   繁体   中英

Unknown Column in subquery why?

Im currently building my self a forum for my school project and this query is listing all the forum categories but im having problem checking if there's a topic/thread thats not been read by the user and then TAG the forum category as having unread messages.

It says "Unknown column 'forum_category.id' in 'field list'" in the subquery... i have checked several examples on subqueries and from what i have seen i should be able to access forum_category.id and use it in the subquery? i don't see what im doing wrong at this point...

Help is much appreciated!

    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

I know tried this, but it only checks the first topic/thread in each category...

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

Who had an idea it would be this hard... i mean its rather simple thing i want to do :(

tables. I believe your problem is in the aliasing of your tables. You've told the engine not to use forum_category but instead use ROOT or subcat. So the SQL doesn't know about a forum_Category (it doesn't see that table in the "FROM". The same problem exists in the outer select too. Change it to root as well.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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