想要基于不出现在另一个表中而从表中选择查询

时间:2010-12-18 15:40:51

标签: mysql

我有三个表:player(playerId, Name)game(gameId, gameName)gameResults(gameId, playerId, results)

我想要从表gameResults中不存在gameId的{​​{1}}中检索行。

我的查询是:

game

我也试过了:

SELECT DISTINCT 
     gameresults.playerId, 
     gameresults.results    
FROM 
     game, 
     gameresults, 
     player
WHERE
     gameresults.playerId = player.playerId
     AND gameresults.gameId NOT IN (
       SELECT game.gameId; FROM game
     )

2 个答案:

答案 0 :(得分:1)

SELECT DISTINCT gameresults.playerId, gameresults.results, game.gameName
FROM gameresults
  LEFT JOIN game ON game.gameId = gameresults.gameId
/ * WHERE gameresults.gameName Is NULL <-- incorrect */
WHERE gameresults.gameId IS NULL;

答案 1 :(得分:0)

SELECT * FROM gameResults 
WHERE gameId NOT IN (SELECT gameid FROM game)

快速而肮脏,但易于理解