如果选择一列

时间:2015-01-13 06:59:45

标签: mysql sql database

我有这样的表

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

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

yr
2008
2007

感谢。

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您可以为Mysql

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

以上查询将仅返回不包含subject ='Chemistry'

的年份

DEMO

答案 2 :(得分:0)

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