我已經創建了SQL的某些表已經,它們低於:如何在SQL中編寫查詢?
CREATE TABLE Courses(
CourseNo INTEGER CHECK(CourseNo>=100 AND CourseNo<=999) PRIMARY KEY,
Title VARCHAR(100) NOT NULL,
)
CREATE TABLE CourseSections(
CourseNo INTEGER CHECK(CourseNo>=100 AND CourseNo<=999),
SectionNo INTEGER,
InstructorNo INTEGER NOT NULL,
Year INTEGER,
Semester INTEGER,
RoomNo INTEGER NOT NULL,
Weekday INTEGER NOT NULL,
StartTime INTEGER NOT NULL,
FinishTime INTEGER NOT NULL,
Capacity INTEGER NOT NULL,
PRIMARY KEY(CourseNo, SectionNo, Year, Semester)
)
CREATE TABLE Instructor(
InstructorNo INTEGER PRIMARY KEY,
FirstName VARCHAR(40) NOT NULL,
LastName VARCHAR(40) NOT NULL
)
CREATE TABLE Students(
StudentNo INTEGER PRIMARY KEY,
FirstName VARCHAR(40) NOT NULL,
LastName VARCHAR(40) NOT NULL,
Year INTEGER,
GPA REAL
)
CREATE TABLE Enrollments(
CourseNo INTEGER CHECK(CourseNo>=100 AND CourseNo<=999),
Year INTEGER,
Semester INTEGER,
SectionNo INTEGER,
StudentNo INTEGER,
Grade REAL,
PRIMARY KEY(CourseNo, Year, Semester, SectionNo, StudentNo)
)
CREATE TABLE Areas(
AreaName VARCHAR(40) PRIMARY KEY
)
CREATE TABLE AreasOfCourse(
CourseNo INTEGER CHECK(CourseNo>=100 AND CourseNo<=999),
AreaName VARCHAR(40),
PRIMARY KEY(CourseNo, AreaName)
)
CREATE TABLE AreasOfInstructor(
InstructorNo INTEGER,
AreaName VARCHAR(40),
PRIMARY KEY(InstructorNo, AreaName)
)
現在,我想找到每門課程的標題,以及該課程,每學期的招生總數,我必須寫一個查詢爲了這。所以我的工作如下:
SELECT c1.Title, COUNT(e1.SectionNo), e1.Semester
FROM Courses c1, Courses c2, Enrollments e1, Enrollments e2
WHERE c1.CourseNo = c2.CourseNo AND e1.Semester = e2.Semester AND
e1.SectionNo <> e2.SectionNo
執行此查詢時出現錯誤。錯誤是:
列「Courses.Title`是在選擇列表中無效,因爲包含在聚合函數或GROUP BY子句是不 。
任何人都可以幫我弄清楚如何解決這個問題嗎?