2011-12-11 2 views
0

현재 내 학교 프로젝트를위한 자체 포럼을 구축 중이며이 쿼리는 모든 포럼 카테고리를 나열하고 있지만 사용자가 읽지 않은 주제/스레드가 있는지 확인한 다음 포럼 카테고리에 읽지 않은 메시지가있는 것으로 태그를 지정하는 경우 문제가 있는지 확인하는 중입니다. .하위 쿼리에 알 수없는 열이 표시되는 이유는 무엇입니까?

그것은 하위 쿼리에서 "알 수없는 열이 '필드 목록'에서 '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 >04546 
       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 

는이 하드 ... 나는 그것보다는 의미하는 것 간단한 일을하고 싶습니다 : (

+1

는 ROOT의 시도로 forum_category 별명했습니다 때문입니다'와 a.forum_id = Root.id' – xQbert

답변

0

테이블. 당신의 문제는 테이블의 앨리어싱에 있다고 생각합니다. 당신은 포럼 _ 카테 고리를 사용하지 말고 대신 ROOT 나 서브 캣을 사용하도록 엔진에 말했기 때문에 SQL은 알지 못합니다 forum_Category에 대해 (그것은 "FROM"에서 해당 테이블을 보지 않습니다. 같은 문제가 외부 선택에도 있습니다. 루트로 변경하십시오.

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 >04546 
       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 
+0

안녕하세요 xQbert는, 나도 그렇게 생각하지만 오류 알 수없는 열 'Root.id'에서 변경 나던 'on clause' 임 : 정말이 하나에 내 머리카락을 당기는 :) 나는 도움에 감사드립니다! – John

+0

글쎄, 내 생각으로는 하나 이상의 왼쪽 조인을 추가하여 서브 쿼리를 없애고 그 밖의 모든 것에 의해 마지막 읽기/쓰기 그룹의 최소/최대 값을 얻는다. 그러나 나는 데이터/테이블을 볼 수 없기 때문에 그것에 고심하고있다. – xQbert

관련 문제