I have a many-to-many relationship between People and Departments since one person can be in many departments.
我与人和部门之间有多对多的关系,因为一个人可以在许多部门。
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
What I want is this:
我想要的是这个:
pName deptName
James Engineering, Research
Mary Research, Communication
Paul Engineering, Research, Marketing
If I do plain LEFT JOINs on the tables using the SQL below, I will get several rows related to one person:
如果我使用下面的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
I have tried various combinations of GROUP_CONCAT
but without luck.
我尝试了GROUP_CONCAT的各种组合,但没有运气。
Any ideas to share?
有什么想法分享?
2 个解决方案
#1
11
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
Output:
输出:
+-------+----------------------------------+
| pName | deptName |
+-------+----------------------------------+
| James | Engineering, Research |
| Mary | Research, Communications |
| Paul | Engineering, Research, Marketing |
+-------+----------------------------------+
3 rows in set (0.00 sec)
#2
0
My solution is:
我的解决方案是:
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
result:
结果:
+-------+----------------------------------+
| pName | deptName |
+-------+----------------------------------+
| James | Engineering, Research |
| Mary | Research, Communications |
| Paul | Engineering, Research, Marketing |
+-------+----------------------------------+
#1
11
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
Output:
输出:
+-------+----------------------------------+
| pName | deptName |
+-------+----------------------------------+
| James | Engineering, Research |
| Mary | Research, Communications |
| Paul | Engineering, Research, Marketing |
+-------+----------------------------------+
3 rows in set (0.00 sec)
#2
0
My solution is:
我的解决方案是:
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
result:
结果:
+-------+----------------------------------+
| pName | deptName |
+-------+----------------------------------+
| James | Engineering, Research |
| Mary | Research, Communications |
| Paul | Engineering, Research, Marketing |
+-------+----------------------------------+