2012-07-31 21 views
0

我有1個表菜單標籤在它。我需要一個查詢來獲取父標籤和UnderMenu label.when我執行此:GROUP_CONCAT &&在一個表上留下外部連接?

SELECT es.Label MenuLabel, m.Label UnderLabel 
    FROM books_menu es 
    LEFT JOIN books_menu m ON es.ID = m.MenuParentID 
    WHERE es.ParentID =1 
    AND m.Type='under' 
    AND es.Type='main' 
    LIMIT 0 , 30 

一切正常,但MenuLabel顯示在每record.Something像這樣:

MenuLabel UnderLabel 
    Home   1 
    Home   2 
    Home   3 
    Contacts  4 

當我執行:

SELECT es.Label MenuLabel, GROUP_CONCAT(m.Label) AS UnderLabel 
    FROM books_menu es 
    LEFT JOIN books_menu m ON es.ID = m.MenuParentID 
    WHERE es.ParentID =1 
    AND m.Type='under' 
    AND es.Type='main' 
    LIMIT 0 , 30 

我得到:

 MenuLabel UnderLabel 
     Home   1,2,3,4 

我怎樣才能得到這樣的:

MenuLabel UnderLabel 
    Home   1,2,3 
    Contacts  4 

謝謝:)

回答

0

你需要有GROUP BY條款:

SELECT es.Label MenuLabel, GROUP_CONCAT(m.Label) AS UnderLabel 
FROM books_menu es 
LEFT JOIN books_menu m ON es.ID = m.MenuParentID 
WHERE es.ParentID =1 
     AND m.Type='under' 
     AND es.Type='main' 
GROUP BY MenuLabel  
LIMIT 0 , 30; 
+0

感謝很多:) – 2012-07-31 07:14:26

+0

歡迎,我希望它的工作原理爲你! – Omesh 2012-07-31 07:18:11