问题 如何连接来自一个字段的数据,在逗号分隔的列表中,在MySQL的多对多关系中?


我与人和部门之间有多对多的关系,因为一个人可以在许多部门。

People          Departments
------          -----------
pID  pName      deptID   deptName
1    James      1        Engineering
2    Mary       2        Research
3    Paul       3        Marketing
                4        Communications

People_Departments
------------------
pID   deptID
1     1
1     2
2     2
2     4
3     1
3     2
3     3

我想要的是这个:

pName  deptName
James  Engineering, Research
Mary   Research, Communication
Paul   Engineering, Research, Marketing

如果我使用下面的SQL在表上执行简单的LEFT JOIN,我将获得与一个人相关的几行:

SELECT people.pName,
       departments.deptName
FROM people
LEFT JOIN people_departments ON people.pID=people_departments.pID
LEFT JOIN departments ON people_departments.deptID=departments.deptID

我尝试了各种组合 GROUP_CONCAT 但没有运气。

有什么想法分享?


12849
2017-07-24 21:33


起源



答案:


    SELECT people.pName,
           GROUP_CONCAT(departments.deptName SEPARATOR ', ') deptName
      FROM people
 LEFT JOIN people_departments 
        ON people.pID = people_departments.pID
INNER JOIN departments 
        ON people_departments.deptID = departments.deptID
  GROUP BY people.pID

输出:

+-------+----------------------------------+
| pName | deptName                         |
+-------+----------------------------------+
| James | Engineering, Research            |
| Mary  | Research, Communications         |
| Paul  | Engineering, Research, Marketing |
+-------+----------------------------------+
3 rows in set (0.00 sec)

11
2017-07-24 21:40



实际上,我的组合都没有用,因为我省略了GROUP BY,这非常重要。我从实际项目中的几个多对多表中获取数据,并设法在您的帮助下获取我想要的数据。再次感谢! - shailenTJ


答案:


    SELECT people.pName,
           GROUP_CONCAT(departments.deptName SEPARATOR ', ') deptName
      FROM people
 LEFT JOIN people_departments 
        ON people.pID = people_departments.pID
INNER JOIN departments 
        ON people_departments.deptID = departments.deptID
  GROUP BY people.pID

输出:

+-------+----------------------------------+
| pName | deptName                         |
+-------+----------------------------------+
| James | Engineering, Research            |
| Mary  | Research, Communications         |
| Paul  | Engineering, Research, Marketing |
+-------+----------------------------------+
3 rows in set (0.00 sec)

11
2017-07-24 21:40



实际上,我的组合都没有用,因为我省略了GROUP BY,这非常重要。我从实际项目中的几个多对多表中获取数据,并设法在您的帮助下获取我想要的数据。再次感谢! - shailenTJ


我的解决方案是:

SELECT people.pName,
GROUP_CONCAT(tmp.deptName SEPARATOR ', ') deptName
FROM people 
LEFT JOIN (SELECT people_departments.pID, departments.deptName FROM people_departments LEFT JOIN departments  ON people_departments.deptID = departments.deptID) as tmp
ON tmp.pID = people.pID
GROUP BY people.pID

结果:

+-------+----------------------------------+
| pName | deptName                         |
+-------+----------------------------------+
| James | Engineering, Research            |
| Mary  | Research, Communications         |
| Paul  | Engineering, Research, Marketing |
+-------+----------------------------------+

0
2018-06-01 10:42