How to concatenate data from one field, in a comma-delimited list, in a many-to-many relationship in MySQL?
SELECT people.pName,
GROUP_CONCAT(departments.deptName SEPARATOR ', ') deptName
FROM people
LEFT JOIN people_departments
ON people.pID = people_departments.pID
LEFT 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)