表1:hosts(主键:hostID) 表2:groups(主键groupID) 中间表:groupmembers
构建子查询(根据分组查出全部gmHostID)
SELECT gmGroupID
,GROUP_CONCAT
(gmHostID
) as hosts
FROM groupmembers
GROUP BY(gmGroupID
)
关联查询
SELECT groupName
,`hosts
` FROM groups
as a
LEFT JOIN (
SELECT gmGroupID
,GROUP_CONCAT
(gmHostID
) as hosts
FROM groupmembers
GROUP BY(gmGroupID
)
) as b
on b
.gmGroupID
= a
.groupID
关联查询结果图:
转载请注明原文地址:https://tech.qufami.com/read-14312.html