怎么写这个SQL?里面的细节

时间:2015-09-05 02:01:27

标签: sql

假设我们有一张表记录了班级所有学生的成绩。表中有两列:1)student_id,2)成绩,成绩的值是单个字母,可以是“A”,“B”,“C”或“F”。如何写一个SQL列出所有从未获得“B”成绩的学生ID(每行一名学生)?感谢。

PS:假设我们正在使用MySQL。

2 个答案:

答案 0 :(得分:1)

您需要一个子查询来完成此任务。您将从表中返回记录,其中student_id不在已收到B的学生列表中。

select student_id, grades
from table_name 
where student_id not in (select student_id from table_name where grade = 'B')

答案 1 :(得分:0)

使用此对不起,您需要定义student_id可以在表格中重复

select student_id from table_name where grades in ('A','B','C')
and student_id not in(
select student_id from table_name where grades = 'B'
)