2012-02-17 195 views
0

你好,我有2个表看起来像这样,左连接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

我敢打赌,你的意思是JOIN ON 'section'.'section_id''' categories'.'parent_section_id' – Andrew 2012-02-17 15:44:47

+2

啊废话!知道这将是愚蠢的! – Udders 2012-02-17 15:45:26

+0

什么是parent_section,一个自引用? “分类”是一棵树吗?在这种情况下,您是否需要取回父母? – 2012-02-17 15:45:39

回答

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;