So I have 5 tables as follows :
CREATE TABLE student (
id serial PRIMARY KEY,
name varchar(255) NOT NULL
-- other columns, constraints, etc...
);
CREATE TABLE teacher (
id serial PRIMARY KEY,
name varchar(255) NOT NULL
-- other columns, constraints, etc...
);
CREATE TABLE course(
id serial PRIMARY KEY,
name varchar(255) NOT NULL
-- other columns, constraints, etc...
);
CREATE TABLE student_course (
student_id integer NOT NULL REFERENCES student(id),
course_id integer NOT NULL REFERENCES course(id)
);
CREATE TABLE teacher_course (
teacher_id integer NOT NULL REFERENCES teacher(id),
course_id integer NOT NULL REFERENCES course(id)
);
and I want to query which student study what course with which teacher.
SELECT s.name, c.name, t.name
FROM student s
JOIN student_course sc ON s.id = sc.student_id
JOIN course c ON sc.course_id = c.id
JOIN teacher_course tc ON c.id = tc.course.id
JOIN teacher t ON tc.teacher_id = t.id
but the result is not what i intended. like 1 student studies one subject with 2 different teachers. It's not the input problem, cos i check and there's no duplicate input in both teacher_course, and student_course. So i think the problem is with my query. Can someone tell me where i went wrong?