2017-03-31 101 views

回答

1

A的等級選擇,左加入等級student_id和等級C,並排除匹配項。

select distinct 
a.student_id 
from grades as a 
    left join grades as c 
    on c.student_id = a.student_id 
    and c.Grade = 'C' 
where a.Grade = 'A' 
and c.student_id is null -- exclude students with C's 

這是demo

2

一個簡單的方法是聚集和一個having子句:

select student_id 
from student 
group by student_id 
having sum(case when grade = 'A' then 1 else 0 end) > 0 and 
     sum(case when grade = 'C' then 1 else 0 end) = 0;