2012-02-17 3 views
0

안녕하세요는 다음과 같이 두 테이블을 가지고 왼쪽 MySQL의 문제에 가입

enter image description here

내가 원하는 것은 MySQL은 다음과 같습니다, 카테고리와 카테고리의 섹션 제목을 얻을되는

,

SELECT `categories`.`category_id`, 
     `categories`.`category_title`, 
     `categories`.`category_created`, 
     `section`.`section_id`, 
     `section`.`section_title`, 
     `categories`.`parent_section` 
FROM (`categories`) 
LEFT JOIN `section` 
ON `section`.`section_id` = `categories`.`category_id` 

그러나 모두 내가 돌아 오는 것은 카테고리 목록과 섹션 목록이 아니라 상위 섹션입니다. 내가 잘못 했니?

+1

난 당신 ON 가입하는 의미 내기 'section'.'section_id' ='categories'.parent_section_id' – Andrew

+2

아! 그것은 어리석은 무엇을 알 것입니다! – Udders

+0

parent_section이란 자체 참조 란 무엇입니까? 'categories'는 나무입니까? 이 경우 부모를 가져 오기위한 귀하의 요구 사항은 무엇입니까? –

답변

2

상위 섹션을 원한다면, 당신은 그 칼럼에 있어야 조인 조건 :

SELECT `categories`.`category_id`, 
     `categories`.`category_title`, 
     `categories`.`category_created`, 
     `section`.`section_id`, 
     `section`.`section_title`, 
     `categories`.`parent_section` 
FROM (`categories`) 
LEFT JOIN `section` 
ON `section`.`section_id` = `categories`.`parent_section` 
0

내가 그것을해야한다고 생각 :

SELECT `categories`.`category_id`, 
     `categories`.`category_title`, 
     `categories`.`category_created`, 
     `section`.`section_id`, 
     `section`.`section_title`, 
     `categories`.`parent_section` 
FROM (`categories`) 
LEFT JOIN `section` 
ON `section`.`section_id` = `categories`.`parent_section` 
0
... 
LEFT JOIN section S ON S.section_id = categories.parent_section;