简体   繁体   English

如何使用以下架构编写此查询?

[英]How to write this query with the following schema?

I have the database schema as follows: 我的数据库架构如下:

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

How can I query(using SQL) all teamID s where the team played against the team1 but not against the team2. 如何查询(使用SQL)团队对着team1而不对着teamID的所有teamID 'team1 and team2 are some data values in the column teamName ? 'team1和team2是teamName列中的一些数据值?

Assuming that there is no game without HomeTeam or GuestTeam . 假设没有HomeTeamGuestTeam没有游戏。 Try this one, 试试这个

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'
        )

All teamIDs where the team played against the team1 but not against the team2 团队与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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM