在MySQL数据库中加入三个表

时间:2013-05-08 21:01:25

标签: mysql

我想加入三张桌子。第一个表(结果)有游戏结果,包含日期,团队a,团队b和最终得分。第二桌(阵容)具有排队的细节,即在给定日期为球队效力的球员列表,即日期,球队,球员1,球员2,球员3等。第三桌(球员)包含关于每个球员的信息,即姓名,身高,重量出生日期等。我想用连接从所有三个表中取出信息。我希望最终值为date,team,player1,player1_height,player1_weight,player2,player2_height,player2_weight等等。三个表格的结构如下:

  • 结果表,日期varchar(50),团队varchar(50),Team_Score int(11)

  • 阵容表,日期varchar(50),Team varchar(50),Player1 varchar(50),
    Player2 varchar(50),Player3 varchar(50),Player4 varchar(50),Player5 varchar(50)

  • 播放器表,firstname varchar(50),height int(11),weight int(11),birthdate varchar(256),

我正在使用以下查询加入前两个表来获取日期,团队,播放器1,播放器2等。

  SELECT 
         result.Date, 
         result.Team, 
         result.Team_Score, 
         lineup.player1, 
         lineup.player2, 
         lineup.player3, 
         lineup.player4, 
         lineup.player5     
  FROM   
         result 
  JOIN
         lineup 
  ON     
         result.date = lineup.date;

我可以做些什么来将这个结果与第三张表联系起来,以获得每一位玩家的身高和体重?谢谢

1 个答案:

答案 0 :(得分:0)

如果我理解,只需为每个玩家加入相同的表格

select  result.Date, result.Team, result.Team_Score
      , lineup.player1, p1.weight
      , lineup.player2, p2.weight
      , lineup.player3, p3.weight
      , lineup.player4, p4.weight
      , lineup.player5, p5.weight
from result, players p1, players p2, players p3, players p4, players p5, lineup 
where result.date = lineup.date
and p1.playerid = lineup.playe1
and p2.playerid = lineup.playe2
and p3.playerid = lineup.playe3
and p4.playerid = lineup.playe4
and p5.playerid = lineup.playe5;