I am trying to pull students and the courses they are registered for from a mysql database. I want to replace the course id with the course name in the output.
I have these tables and columns:
table: users_to_courses
columns: users_LOGIN and courses_ID
table: users
columns: login, name and surname
table: courses
columns: id and name
The tables tie together on the following:
users_to_courses.users_LOGIN = users.login
users_to_courses.courses_ID = courses.id
I am trying to get output of surname, name, courses (by name and not id) so it would look like Smith, John Chemistry, Physics, Composition
Here is what I am trying unsuccessfully:
SELECT
users.surname,
users.name,
users.login,
users_to_courses.users_LOGIN,
(select group_concat (courses.name) from courses
where users_to_courses.users_LOGIN = users.login and users_to_courses.courses_ID =
courses.id
GROUP BY users.surname) as courses
from courses join users_to_courses
on courses.id = users_to_courses.courses_ID
Try this