To achieve this, you want to use a mysql function called group_concat. Your query will look something like this:
SELECT courseName, group_concat(teacherName) FROM teacher, course, courses_has_teachers WHERE courses_has_teachers.teacher_id = teacher.id AND course.id = courses_has_teachers.course.id GROUP BY courseName
I rewrote this query in ANSI-92 format, which you may not be familiar with, but can make queries with multiple connections. SO is much easier to read:
SELECT courseName, group_concat(teacherName) FROM teacher join course on courses_has_teachers.teacher_id = teacher.id join courses_has_teachers on course.id = courses_has_teachers.course.id WHERE // Any conditions you want on the query GROUP BY courseName
Also, you might want to read the long Q&A that I wrote, which may come in handy.
source share