[英]GROUP_CONCAT && left outer join on 1 table?
I've got 1 table with Menu labels in it.I need with a query to get Parent label and UnderMenu label.when i execute this: 我有1个带有Menu标签的表。执行此操作时需要查询以获取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
everything is OK but MenuLabel show on every record.Something like this: 一切正常,但MenuLabel会在每条记录上显示。
MenuLabel UnderLabel
Home 1
Home 2
Home 3
Contacts 4
When i execute : 当我执行时:
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
i get: 我得到:
MenuLabel UnderLabel
Home 1,2,3,4
How can i get this: 我怎么能得到这个:
MenuLabel UnderLabel
Home 1,2,3
Contacts 4
Thank you:) 谢谢:)
You need to have GROUP BY
clause: 您需要具有
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.