无法弄清楚如何在加入桌子时将玩家和家庭玩家分开

时间:2017-09-15 08:53:47

标签: mysql sql

我有两个表,一个是schedules表,另一个是四分卫的stats表。

我已经加入了他们......


    SELECT statsSchedules.*
           , awayTeam.fullName     awayQBName
           , awayTeam.passComp     awayPlayerPassComp
           , awayTeam.passAtt      awayPlayerPassAtt
           , awayTeam.passCompPct  awayPlayerPassCompPct
           , awayTeam.passYds        awayPassYds
           , awayTeam.passInts     awayPlayerPassInts
           , awayTeam.passLongest  awayPlayerLongestPass
           , awayTeam.passPts        awayPlayerPassPts
           , awayTeam.passerRating awayPlayerPassRating
           , awayTeam.passSacks    awayQBTimesSacked
           , awayTeam.passTDs        awayQBTds
           , awayTeam.statId       awayStatId

           , homeTeam.fullName     homeQBName
           , homeTeam.passComp     homePlayerPassComp
           , homeTeam.passAtt      homePlayerPassAtt
           , homeTeam.passCompPct  homePlayerPassCompPct
           , homeTeam.passYds        homePassYds
           , homeTeam.passInts     homePlayerPassInts
           , homeTeam.passLongest  homePlayerLongestPass
           , homeTeam.passPts        homePlayerPassPts
           , homeTeam.passerRating homePlayerPassRating
           , homeTeam.passSacks    homeQBTimesSacked
           , homeTeam.passTDs        homeQBTds
           , homeTeam.statId        homeStatId

      FROM statsSchedules
    INNER JOIN statsPassing AS awayTeam
        ON statsSchedules.scheduleId =  awayTeam.scheduleId
    INNER JOIN statsPassing AS homeTeam
        ON statsSchedules.scheduleId =  homeTeam.scheduleId
     WHERE statsSchedules.scheduleId=542769239
     ORDER BY statsSchedules.weekIndex ASC

这样做会导致多次重复统计和名称。

What my SQL query returns

我尝试使用SELECT DISTINCT statsSchedules.*,但似乎没有做任何事情。

2 个答案:

答案 0 :(得分:0)

你需要加入statsPassing on match and team(我猜你自己并没有说出你想要的东西)。例如:

select *--the stats you need
FROM statsSchedules
JOIN statsPassing AS awayTeam ON statsSchedules.scheduleId=awayTeam.scheduleId and statsSchedules.awayTeamID=awayTeam.teamID
JOIN statsPassing AS homeTeamON statsSchedules.scheduleId=homeTeam.scheduleId and statsSchedules.homeTeamID=homeTeamTeam.teamID
WHERE statsSchedules.scheduleId=542769239
ORDER BY statsSchedules.weekIndex ASC

答案 1 :(得分:0)

由于来自user3532232

的评论,我能够解决我的问题

我很接近,我需要的是添加

    AND statsSchedules.awayTeamId=awayTeam.teamId
    AND statsSchedules.scheduleId=awayTeam.scheduleId

    AND statsSchedules.homeTeamId=homeTeam.teamId 
    AND statsSchedules.scheduleId=homeTeam.scheduleId

添加这两个AND函数后,查询会在awayTeamhomeTeam列中显示正确的播放器,而不会重复。