0

我的数据库架构如下:

Teams(teamID,teamName,home,leagueName)
Games(gameID,homeTeamID,guestTeamID,date)

我如何查询(使用 SQL)teamID团队与 team1 但不与 team2 对抗的所有 s。'team1 和 team2 是列中的一些数据值teamName吗?

4

2 回答 2

0

球队与 team1 比赛但未与 team2 比赛的所有 teamID

SELECT t1.teamName AS 'Home', t2.TeamName AS 'Guest', g.Date
FROM
(
   SELECT *
   FROM Games
   WHERE (homeTeamID  = 'Team 1' OR guestTeamID  =  'Team 1')
     AND (homeTeamID <> 'Team 2' OR guestTeamID <> 'Team 1')
) g ON t.teamID = hg.homeTeamID
INNER JOIN teams t1 ON g.homeTeamID  = t1.teamId 
INNER JOIN teams t2 ON g.guestTeamID = t2.teamId 
于 2012-10-08T08:12:50.903 回答
0

假设没有HomeTeam或就没有游戏GuestTeam。试试这个,

SELECT  a.gameID,
        b.TeamName AS HomeTeam,
        c.TeamName AS GuestTeam,
        a.`date`
FROM    GAMES a
        INNER JOIN Teams b
            ON a.homeTeamID = b.teamID
        INNER JOIN Teams c
            ON a.guestTeamID = c.teamID
WHERE   (
            b.TeamName = 'Team1' AND
            c.teamName <> 'Team2'
        )
        OR
        (
            c.TeamName = 'Team1' AND
            b.teamName <> 'Team2'
        )
于 2012-10-08T08:00:08.610 回答