select 来自一列,如果

select from one column if

我有table这样的

yr      subject     winner

2008    Economics   Paul Krugman
2008    Medicine    Harald zur Hausen
2007    Peace       Al Gore
2007    Physics     Albert Fert
2006    Chemistry   Roger D. Kornberg
2006    Physics     George F. Smoot
2005    Chemistry   Yves Chauvin
2005    Economics   Thomas C. Schelling

我需要 select 年,如果只有 'subject != 'Chemistry' 我的意思是我希望查询输出像

yr
2008
2007

谢谢。

select distinct yr from table_name where subject not like 'chemistry' 

您可以为 Mysql

编写如下查询
select yr
from t 
group by yr
having sum(subject ='Chemistry') = 0

以上查询将 return 仅不包含主题的年份 ='Chemistry'

DEMO

SELECT DISTINCT yr FROM table_name WHERE yr not in (select yr FROM table_name WHERE subject ='Chemistry')