mysql获取多对多关联表的关联id

tech2023-12-20  73

表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

关联查询结果图:

最新回复(0)