如何在sql中专门获取值?

时间:2016-06-13 17:52:22

标签: mysql

所以我正在写一个sql代码,让玩家只在第2组:

SELECT PlayerID from Player 
WHERE EXISTS 
( SELECT * from Team WHERE team.PlayerID = Player.PlayerID  AND Team.TeamID = 't002');

问题是,我忘了可能有一些玩家也在其他团队的情况下,所以任何想法如何解决?

2 个答案:

答案 0 :(得分:1)

使用而不是

SELECT PlayerID from Player 
WHERE PlayerID in ( 
        SELECT  team.PlayerID from Team WHERE  Team.TeamID = 't002'
)
AND not in (
       SELECT  team.PlayerID from Team WHERE  Team.TeamID != 't002'
)
;

答案 1 :(得分:0)

应该使用加入

SELECT PlayerId FROM Player JOIN Team 
WHERE Team.PlayerId = Player.PlayerId AND Team.TeamId = 't002' 
AND NOT IN (SELECT team.PlayerID FROM Team 
WHERE Team.TeamId != 't002' );