加入三个联结表

Joining three junction tables

我想弄明白这个问题时头疼!

我有三张桌子 -球员 -团队 -游戏

具有两列的三个联结表。 -player_teams -teams_games -player_games

我需要列出游戏中的所有玩家(例如:Game_id = 变量中的 111)未分配到团队(在此游戏中)。我称他们为孤儿玩家

基本上获取游戏中的团队,获取他们的球员并反向匹配 Games_players。或者我想反过来。

我试了两天都不行!

谢谢!

/J p.s 在我发布这篇文章之后,我已经走到这一步了,但它似乎很复杂!

SELECT * from players
JOIN
(SELECT DISTINCT games_players.player_id from games_players
Left JOIN
(Select team_players.player_id p1 from team_players 
inner join (Select * from games_teams where games_teams.game_id = :P1) AS tm1 ON team_players.team_id = tm1.team_id) As f1
On games_players.player_id = f1.p1
where p1 is null) as q1
on players.player_id = q1.player_id

由于给出了比赛,您可以只看比赛中的球员和比赛中的球队球员:

select *
from players
where player_id in
(
  select player_id
  from player_games
  where game_id = 111
)
and player_id not in
(
  select pt.player_id
  from player_teams pt
  join teams_games tg using (team_id)
  where tg.game_id = 111
);