我必须列出学生姓名和他/她所参加的课程数量(包括尚未上课的学生)
以下是我的5张表中的3张,不应该需要教师和课程表
--Student Table
CREATE TABLE Student(
Std_ID NUMBER(4) CONSTRAINT Student_ID_pk PRIMARY KEY,
Std_FN VARCHAR2(20),
Std_LN VARCHAR2(20),
Std_City VARCHAR2(20),
Std_State VARCHAR2(20),
Std_Zip NUMBER(5),
Std_Major VARCHAR2(10),
Std_Class VARCHAR2(2),
Std_GPA NUMBER(3,2) CONSTRAINT Student_GPA_cc CHECK (Std_GPA<= 4.0 AND Std_GPA>=0));
INSERT INTO Student
VALUES('101','Joe','Smith','Eau Clare','WI', '18121','IS','FR','3.0');
INSERT INTO Student
VALUES('102','Jenny','Sneider','Eau Clare','WI', '98011','IS','JR','3.2');
INSERT INTO Student
VALUES('103','Dan','Robinson','Sartell','MN', '98042','IS','JR','3.5');
INSERT INTO Student
VALUES('104','Sue','Williams','St.Cloud','MN', '56301','ACCT','SR','3.2');
-------------------------------------------------------------------------------
--Offering Table
CREATE TABLE Offering(
Offer_No NUMBER(4) CONSTRAINT Offer_No_pk PRIMARY KEY,
Course_No VARCHAR2(10) CONSTRAINT Course_No_fk REFERENCES Course(Course_No),
Off_Term VARCHAR2(7),
Off_Year NUMBER(4),
Off_Loca VARCHAR2(10),
Off_Time Varchar2(8),
Off_Day VARCHAR2(7),
Fac_SSN NUMBER(4) CONSTRAINT Fac_SSn_fk REFERENCES Faculty(Fac_ID));
INSERT INTO OFFERING
VALUES('2201', 'IS 250', 'Spring', '2000', 'BB260', '10:30am', 'MWF', '9002');
INSERT INTO OFFERING
VALUES('2202', 'IS 250', 'Spring', '1999', 'BB118', '8:00am', 'TTH', '9002');
INSERT INTO OFFERING
VALUES('2203', 'IS 350', 'Fall', '2001', 'BB260', '9:30am', 'TTH', '9001');
INSERT INTO OFFERING
VALUES('2204', 'IS 351', 'Fall', '2001', 'BB315', '12:30pm', 'TTH', '9003');
INSERT INTO OFFERING
VALUES('1101', 'ACCT 291', 'Fall', '2000', 'BB320', '12:30pm', 'MWF', '9010');
INSERT INTO OFFERING
VALUES('2205', 'IS 443', 'Fall', '2002', 'BB216', '12:30pm', 'MWF', '9003');
---------------------------------------------------------------------------------
--Enrollment Table
CREATE TABLE Enrollment(
Std_ID NUMBER(4) CONSTRAINT Enroll_Std_ID_fk REFERENCES Student(std_ID),
Offer_No NUMBER(4) CONSTRAINT Enroll_Offer_No_fk REFERENCES Offering(Offer_No),
Enr_Grade Char(1) CONSTRAINT Enroll_grade_cc CHECK (Enr_Grade IN('A','B','C','D','F')),
CONSTRAINT Enroll_pk PRIMARY KEY (Std_ID,Offer_No));
INSERT INTO ENROLLMENT
VALUES('101', '2201', 'A');
INSERT INTO ENROLLMENT
VALUES('101', '2203', 'B');
INSERT INTO ENROLLMENT
VALUES('102', '2203', 'C');
INSERT INTO ENROLLMENT
VALUES('103', '2203', 'B');
INSERT INTO ENROLLMENT
VALUES('103', '2201', 'C');
INSERT INTO ENROLLMENT
VALUES('103', '1101', 'B');
这是我现在提出的问题。它仍然不显示尚未注册课程的学生。我怎样才能解决这个问题,以便它显示0个课程。谢谢。
--Q2
SELECT Count(*) as Num_Courses, Std_FN, Std_LN
FROM Enrollment, Student
WHERE Enrollment.Std_ID = Student.Std_ID
Group by Enrollment.Std_ID, Std_FN, Std_LN
Having Count(*)>= 0;
答案 0 :(得分:1)
您需要OUTER
加入此处。没有注册的学生将从注册表中返回NULL
个值,因此它的计数将为0.并且您修改的查询是
SELECT Count(Enrollment.Std_ID) as Num_Courses, Std_FN, Std_LN
FROM Enrollment, Student
WHERE Student.Std_ID = Enrollment.Std_ID(+)
Group by Student.Std_ID, Std_FN, Std_LN
Having Count(*)>= 0;
答案 1 :(得分:1)
您的查询具有内部联接,这意味着Std_ID
需要位于两个表中,否则行将不会包含在内。换句话说,没有课程的学生不是计算结果集的一部分。
改为使用左连接,并依靠Enrollment.Std_ID
。如果学生没有匹配的Enrollment
行,那么Enrollment.Std_ID
将为空,因此不会计算,这会导致该学生为零 - 这就是您想要的。
还有一件事:我非常确定甲骨文会要求你在Std_ID
条款中SELECT
,如果你正在按照它进行分组。
SELECT COUNT(Enrollment.Std_ID) AS Num_Courses, Student.Std_ID, Std_FN, Std_LN
FROM Student
LEFT JOIN Enrollment ON Student.Std_ID = Enrollment.Std_ID
GROUP BY Student.Std_ID, Std_FN, Std_LN