连接两个表和两个联结表

Joining two tables and two junction tables

我有这些表:

联结表:

基本上每场比赛我都想看球队人数和球员人数。

游戏名称-队伍数量-玩家数量

使用内部连接,我成功地加入了团队的游戏,但没有加入玩家。我相信它涉及多个 select 语句?

您可以在 games_teams 结点 table 的帮助下加入 gamesteams。然后,您可以加入 teams_players 以获取玩家 ID。请注意,对于此查询,您甚至不需要 players table,因为您只需要他们的号码:

SELECT   game_name, 
         COUNT(DISTINCT gt.team_id) AS number_of_teams,
         COUNT(DISTINCT tp.player_id) AS number_of_players
FROM     games g
JOIN     games_teams gt on g.game_id = gt.game_id
JOIN     teams t ON gt.team_id = t.team_id
JOIN     teams_players tp ON t.team_id = tp.team_id
GROUP BY game_name