我正在为练习做几个SQL查询,并且遇到了一些问题。
我正在努力寻找'SYS'跟踪'CptS'专业的学生参加的不同课程,然后返回courseno和学分。结果应根据courseno进行分类。
可以从this文件创建表格。它是一个zip文件,包含用于创建表的SQL。
他们需要创建的顺序如下:课程,曲目,学生,注册,先决条件,跟踪要求
我无法获得仅仅在赛道要求中的课程数量而不是所有已注册的课程。
到目前为止,这是我的SQL语句:
SELECT student.sname, student.sid, student.major FROM enroll, student, trackrequirements
WHERE student.trackcode = trackrequirements.trackcode AND student.major = trackrequirements.major AND enroll.sid = student.sid AND enroll.courseno = trackrequirements.courseno
GROUP BY student.sname, student.sid, student.major
HAVING COUNT(trackrequirements.courseno) = COUNT(enroll.courseno)
ORDER BY student.sname
如果有人可以给我一些建议或帮助,我们将不胜感激。
以下是架构:
CREATE TABLE Course (
courseno VARCHAR(7),
credits INTEGER NOT NULL,
enroll_limit INTEGER,
classroom VARCHAR(10),
PRIMARY KEY(courseNo),
);
CREATE TABLE Tracks (
major VARCHAR(7),
trackcode VARCHAR(10),
title VARCHAR(30),
PRIMARY KEY(major, trackcode)
);
CREATE TABLE Student (
sID CHAR(8),
sName VARCHAR(30),
major VARCHAR(10),
trackcode VARCHAR(10),
PRIMARY KEY(sID),
FOREIGN KEY (major,trackcode) REFERENCES Tracks(major,trackcode)
);
CREATE TABLE Enroll (
courseno VARCHAR(7),
sID CHAR(8),
grade FLOAT NOT NULL,
PRIMARY KEY (courseNo, sID),
FOREIGN KEY (courseNo) REFERENCES Course(courseNo),
FOREIGN KEY (sID) REFERENCES Student(sID)
);
CREATE TABLE Prereq (
courseNo VARCHAR(7),
preCourseNo VARCHAR(7),
PRIMARY KEY (courseNo, preCourseNo),
FOREIGN KEY (courseNo) REFERENCES Course(courseNo),
FOREIGN KEY (preCourseNo) REFERENCES Course(courseNo)
);
CREATE TABLE TrackRequirements (
major VARCHAR(7),
trackcode VARCHAR(10),
courseNo VARCHAR(7),
PRIMARY KEY (major,trackcode,courseNo),
FOREIGN KEY (major,trackcode) REFERENCES Tracks(major,trackcode),
FOREIGN KEY (courseNo) REFERENCES Course(courseNo)
);
以下是一些数据的外观:
感谢您阅读
课程:
('CptS121',4,24,'Sloan175'), ('CptS122',4,25,'Sloan175'), ('CptS223',3,25,'Sloan150'), ('CptS260',3,3,'Sloan150'), ('CptS322',3,20,'Sloan169'), ('CptS323',3,19,'Sloan169')
报名参加:
('MATH115','12584189',3),
('MATH115','12534189',2),
('MATH115','12524189',4),
('CE211','12584189',3.5),
('CE211','12534189',2.5)
前提条件:
('CptS122','CptS121'),
('CptS223','CptS122'),
('CptS322','CptS223'),
('CptS323','CptS322')
学生:
('12584489','Aaron','ME',NULL),
('12584189','Macy','CE',NULL),
('12583889','John','MATH',NULL)
('12583589','Alice','CptS','SYS')
TrackRequirements:
('CptS','SE','CptS121'),
('CptS','SE','CptS122'),
('CptS','SE','CptS223'),
('CptS','SE','CptS260')
曲目:
('CptS','SE','Software Engineering Track'),
('CptS','SYS','Systems Track'),
('CptS','G','General Track'),
('EE','CE', 'Computer Engineering Track'),
('EE','ME', 'Microelectronics Track'),
('EE','POW','Power Track');
这是我目前的输出:
sname sid major
Ali 12582389 CptS
Alice 12583589 CptS
Bill 12581189 CptS
Jack 12584789 CptS
这是所需的输出:
sid sname major
12584789 Jack CptS
显示的数据只是总数据的一部分,如果您想查看完整数据,请告诉我。
感谢任何帮助,如果我需要改变其他任何内容,请告诉我。
感谢您阅读
答案 0 :(得分:1)
我不得不调整您的数据以保持关系完整性。这就是你需要的东西,如下面的查询。当然,您需要使用与问题相关的条件替换where类。您可以转到此DBFiddle查看以下查询的实时运行。构建此类查询的经验法则是遵循以下基本步骤:
这绝对是解释查询构建的非常外行的方式,但有一个流程有助于。如果您有疑问,请告诉我。
select distinct s.sname, c.courseno, c.credits from trackrequirements t
JOIN enroll e ON e.courseno = t.courseno
JOIN student s ON s.sID = e.SID AND t.major = s.major AND t.trackcode = s.trackcode
JOIN course c ON c.courseno = t.courseno
where t.major = 'CptS' AND t.trackcode = 'SE'
ORDER BY c.courseno;