从多行中选择DISTINCT,如果包含具有不需要值的行,则不要返回

时间:2012-11-22 06:30:41

标签: mysql

ID_Number|Subject_Code|Grade
000001   |Math        |Dropped
000001   |English     |Passed
000001   |Physics     |Passed
000002   |Math        |Passed
000002   |English     |Passed
000002   |Physics     |Passed
000003   |Math        |Passed
000003   |English     |Passed
000003   |Physics     |Passed

现在我希望SELECT DISTINCT ID_Number其中所有subject_code的等级都应为Passed,查询应返回000002和000003,因为000001有1个Dropped主题,其他等级不包括'Withdrawn'和'Failed ”。

1 个答案:

答案 0 :(得分:3)

SELECT DISTINCT ID_Number
FROM   <table>
WHERE  ID_Number not in(select ID_Number from <table> where Grade <> 'Passed')

SQL fiddle demo