-1

我有一张看起来像这样的桌子

YR    Game
1960  football
1961  football
1962  football
1962  tennis
1963  football
1963  golf
1965  football
1965  tennis 
1965  golf
1967  tennis
1967  volleyball

如何使用 join 编写查询以查找必须将足球作为比赛而不是网球的年份..

4

3 回答 3

3

您可以使用 WHERE 过滤器来获得结果:

select yr, game
from yt
where game = 'Football'
  and yr not in (select yr
                 from yt
                 where game = 'tennis');

请参阅带有演示的 SQL Fiddle

于 2013-05-30T22:13:29.370 回答
0

您可以使用连接来过滤它(缺点是您需要两列上的索引):

SELECT YR FROM table AS a
LEFT JOIN table AS b ON b.YR = a.YR AND b.Game = 'tennis'
WHERE a.Game = 'football' AND b.Game IS NULL
于 2013-05-30T22:14:21.803 回答
0

也许这个查询?

select game_year from table_game a left outer join table_game b on a.game_year= 
b.game_year and b.game = 'tennis' where a.game = 'football' and coalesce(b.game, '') = ''
于 2013-05-30T22:14:37.630 回答