帮助基本的SQL QUERY

时间:2011-03-06 06:25:08

标签: sql postgresql

我为schoool项目创建了几个表。感兴趣的表是

                //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)

当我尝试以下查询时出现问题

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;

我收到此错误 SQL错误:

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

提前致谢

1 个答案:

答案 0 :(得分:2)

你的where子句中有weights_subjects_courses,但它不在from子句中。无论何时在where子句中连接表,都需要在from子句中包含它们。

因此,只需将weights_subjects_courses添加到from子句中即可修复该错误。