2011-09-17 36 views
0

考慮爲SQL以下模式:如何獲得列中常見的類SQL

Student (StudID, StudName, DeptID, Age, Gpa); 

Course (CourseID, CourseName, InstructorID); 

Department (DeptID, DeptName, Location); 

Instructor (InstructorID, InstructorName, DeptID); 

Section (SectionID, SectionName, Time, RoomID, CourseID, InstructorID); 

Room (RoomID, RoomName, Location); 

Enrolled (StudID, SectionID); 

問:如何發現無論是在公共休息室見面或有五個以上的學生全部段的名稱就讀?

+1

運行它,你有什麼到目前爲止已經試過? – NaveenBhat

回答

0

那麼我不知道它是否會工作:)

通用名;

select st.StudName as names from Student as st inner join Departmant as d 
on st.DeptID = d.DeptID inner join Instructor as i 
on i.DeptID = d.DeptID inner join Course as c 
on c.InstructorID = i.InstructorID inner join Section as s 
on s.InstructorID = i.InstructorID inner join Room as r 
on r.RoomID = s.RoomID inner join Enrolled as e 
on e.StudID = st.StudID; 

超過5名學生報名(Something experimental :);

select st.StudName as names from Student as st inner join Departmant as d 
on st.DeptID = d.DeptID inner join Instructor as i 
on i.DeptID = d.DeptID inner join Course as c 
on c.InstructorID = i.InstructorID inner join Section as s 
on s.InstructorID = i.InstructorID inner join Room as r 
on r.RoomID = s.RoomID inner join Enrolled as e 
on e.StudID = st.StudID where count(e.StudID = st.StudID)>4; 
0

如果您使用的是SQL-Server,您可以寫這樣的查詢(我沒有測試它,所以我不能說這工程100%,但我希望它讓你的想法。):

SELECT SectionName 
     FROM Section 
       WHERE SectionID IN --Students in common room. 
        (
         SELECT SectionID FROM Section 
          INNER JOIN Instructor ON Section.SectionID = Instructor.InstructorID --Section to which the Instructor belongs 
          INNER JOIN Department ON Department.DeptID = Instructor.DeptID --Department to which the Instructor belongs 
          INNER JOIN Room ON Room.Location = Department.Location --Room to which the Department belongs 
        ) 
        OR --Student Enrollment greater than 5. 
        (
         (SELECT COUNT(StudID) FROM Student 
          INNER JOIN Enrolled ON Student.StudID = Enrolled.StudID 
          INNER JOIN Section ON Section.SectionID = Enrolled.SectionID) >= 5 
        ) 
0

喜MATEEN我HAIDER這裏 檢查這一個 問:6。查找在New-8教室會面或者有五個或五個以上學生參加的所有部分的名字。

答:

select sectionName 
from student as S, Enrolled as E, Section as Se 
where S.studId=E.studId AND E.sectionID=se.SectionID 
group by sectionName 
having count(*)>=3 

UNION 

SELECT sectionName 
FROM SECTION S, ROOM R 
WHERE S.RoomID=R.ROOMID AND R.RoomName='new2' 

,這是正確的查詢我已經分貝的

+0

它的工作.thanks :) –

相關問題