如果我有这样的桌子:

yr          subject
1960    Physics
1960    Chemistry
1961    Physics
1962    Chemistry

我要的是物理而不是化学
如果我尝试这个,它不会给我正确的结果。
select yr from table where subject = "Physics" and
yr NOT IN (select yr from table where subject = "Chemistry")

最佳答案

你的问题是,你两次返回两个物理奖的年份。添加DISTINCT关键字只提供年份(根据question 3A on this site

SELECT DISTINCT yr FROM nobel
WHERE
  subject = 'Physics' AND
  yr NOT IN (SELECT yr FROM nobel WHERE subject = 'Chemistry');

关于mysql - 子查询mysql,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2327570/

10-09 19:36