我有8张桌子-
Course(Courseno,Coursename,dept)
Section(Courseno,Sectionno),
Student(ssn,firstname,lastname,street,zip,city,state),
Enrolls(ssn,Sectionno,Courseno),Exam(Courseno,Sectionno,Examno),
Classroom(roomno,building,capacity),
conducted_in(roomno,building,Courseno,Sectionno,Examno)
Takes(ssn,Courseno,Sectionno,Examno,result)我正在写两个问题。第一个查询是获取有两个或更多学生的课程名称和课程编号。我把它写成
mysql> SELECT c.CourseNo,Course_Name FROM STUDENT s INNER JOIN ENROLLS e
ON s.SSN=e.SSN INNER JOIN COURSE c ON e.CourseNo=c.CourseNo WHERE e.SSN>=2;我得到的输出
+----------+-----------------------+
| CourseNo | Course_Name |
+----------+-----------------------+
| CSC11 | Computer Architecture |
| CSC12 | VLSI |
| CSC12 | VLSI |
+----------+-----------------------+
3 rows in set (0.00 sec)对于第二个查询,它表示要获取关于在构建‘RVR’时所进行的考试的信息(考试号、课程号和节号)。再加上这个房间的容量,房间号和建筑名称。我把查询写成-
mysql> SELECT e.ExamNo,e.CourseNo,e.SectionNo,cr.RoomNo,cr.Building FROM EXAM e
INNER JOIN CONDUCTED_IN ci ON ci.ExamNo=e.ExamNo INNER JOIN CLASS_ROOM cr
ON cr.Building=ci.Building='RVR';并得到了输出
空集,1个警告(0.00秒)
尽管RVR大楼在桌子上
既然我没有得到预期的产出,我又犯了什么错误??请帮帮我。
发布于 2020-04-20 05:18:46
为您的Question 2尝试以下操作,您还没有在查询中添加WHERE条件。这就是on cr.Building=ci.Building='RVR'的问题
select e.ExamNo,
e.CourseNo,
e.SectionNo,
cr.RoomNo,
cr.Building
from EXAM e
JOIN CONDUCTED_IN ci
on ci.ExamNo=e.ExamNo
JOIN CLASS_ROOM cr
on cr.Building=ci.Building
WHERE cr.Building = 'RVR';对于Question 1,您忘记使用DISTINCT,GROUP BY也可以实现相同的结果。
SELECT
DISTINCT c.CourseNo,
Course_Name
FROM STUDENT s
JOIN ENROLLS e
ON s.SSN=e.SSN
JOIN COURSE c
ON e.CourseNo=c.CourseNo
WHERE e.SSN>=2;https://stackoverflow.com/questions/61315537
复制相似问题