2012-02-17 143 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;