确定记录的出现次数

时间:2010-09-26 01:36:19

标签: sql mysql

我有两张桌子 - 学生和面试 - 有价值观:

student
----------
sID  
sName  

interview
----------
sID  
date  
outcome = (OFFER, NOOFFER, HIRED)  

我必须列出任何从未收到录取通知但已接受过五次以上面试的学生的sID。

我无法弄清楚如何确定学生是否有超过5次面试。我知道你可以只计算记录,看看sID是否列出超过5次,但我不确定如何格式化它。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:4)

使用:

SELECT s.sid
  FROM STUDENT s
 WHERE EXISTS(SELECT NULL -- more than five interviews
                FROM INTERVIEW i 
               WHERE i.sid = s.sid
            GROUP BY i.sid
              HAVING COUNT(*) > 5)
   AND NOT EXISTS(SELECT NULL -- never received an offer
                    FROM INTERVIEW i
                   WHERE i.sid = s.sid
                     AND i.outcome = 'OFFER')

答案 1 :(得分:0)

select s.sID,s.sName from
( select sID,count(0) as numInterviews 
  from interview 
  where sID not in (select sID from interview where outcome='OFFER') 
  group by sID
) as interviewCounts
join student s on s.sID=interviewCounts.sID
where interviewCount>5