2011-03-06 63 views
1

我爲schoool項目創建了幾個表。感興趣的表是有關基本SQL的幫助QUERY

   //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... 
                  ^

在此先感謝

回答

2

你在where子句中有weights_subjects_courses,但它不在從條款。無論何時在where子句中連接表,都需要將它們包含在from子句中。

所以,只需在您的from子句中添加weights_subjects_courses以修復該錯誤。

+0

「無論何時在where子句中加入表,都需要將它們包含在from子句中。」謝謝:) – 2011-03-06 06:37:29

+0

隨時,我想這只是一個疏忽;) – 2011-03-06 06:47:54