如何在SQL中的列中获取公共类别

时间:2011-09-17 06:55:40

标签: 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);

问:如何找到在公共场所见面或有五个或更多学生注册的所有部分的名称?

3 个答案:

答案 0 :(得分: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名学生注册(实验性的东西:);

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;

答案 1 :(得分: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
                    )

答案 2 :(得分:0)

嗨Mateen me 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'

这是我在Db上运行它的正确查询