我有3张看起来像这样的表:
STUDENT(*snum: integer, sname: string, deptid: integer, slevel: string, age: integer)
CLASS(*cname: string, meets_at: date, room: string, fid: integer)
ENROLLED(*snum: integer, *cname: string)
我正在努力找到学生的名字(sname),他们在所有房间(CLASS的房间)上课。我该怎么做?
答案 0 :(得分:1)
select sname from STUDENT
join ENROLLED on STUDENT.snum = ENROLLED.snum
join CLASS on ENROLLED.cname = CLASS.cname
group by sname
having count(distinct room) = (select count(distinct room) from CLASS)
SQLfiddle:http://sqlfiddle.com/#!2/71cba/1/0
您应该将ROOMS作为单独的表格,以避免房间名称输入不一致的问题。