我有1个带有菜单标签的表。我需要一个查询来获取Parent标签和UnderMenu标签。当我执行此操作时:
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显示在每条记录上。这样的事情:
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 :(得分: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;