Dr Deo
Dr Deo

Reputation: 4848

help with basic SQL QUERY

i have created a few tables for a schoool project. The tables of interest are

                //table courses
        CREATE TABLE courses(courseId SERIAL PRIMARY KEY, facultyId REFERENCES faculties(facultyId), courseName TEXT);

                //table weights
    CREATE TABLE weights(weightId SERIAL PRIMARY KEY, weightName TEXT, weight INTEGER);

//table subjects
CREATE TABLE subjects(subjectId SERIAL PRIMARY KEY, subjectName TEXT);
        //table weights_subjects_courses
    CREATE TABLE weights_subjects_courses(courseId integer REFERENCES courses(courseId), weightId integer REFERENCES weights(weightId), subjectId integer REFERENCES subjects(subjectId)

The problem comes when i try the following query

SELECT * FROM courses, subjects, weights WHERE courses.courseId= weights_subjects_courses.courseId AND subjects.subjectId= weights_subjects_courses.subjectId AND weights.weightId= weights_subjects_courses.weightId ORDER BY courseName;

i get this error SQL error:

ERROR:  missing FROM-clause entry for table "weights_subjects_courses"
LINE 1: ...ourses, subjects, weights WHERE courses.courseId= weights_su...
                                                             ^

Thanks in advance

Upvotes: 1

Views: 69

Answers (1)

Shan Plourde
Shan Plourde

Reputation: 8736

You have weights_subjects_courses in your where clause, but it's not in the from clause. Whenever you join tables in your where clause, you need to also include them in your from clause.

So just add weights_subjects_courses into your from clause to fix that error.

Upvotes: 2

Related Questions