我有如下数据库模式:
Teams(teamID,teamName,home,leagueName)
Games(gameID,homeTeamID,guestTeamID,date)
如何查询(使用SQL)
所有teamID
s团队对阵球队1而不是对阵球队2。 'team1和team2是teamName
列中的一些数据值?
答案 0 :(得分: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'
)
答案 1 :(得分:0)
球队对阵球队1但不反对的所有球队ID team2
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