我的数据库架构如下:
Teams(teamID,teamName,home,leagueName)
Games(gameID,homeTeamID,guestTeamID,date)
我如何查询(使用 SQL)teamID
团队与 team1 但不与 team2 对抗的所有 s。'team1 和 team2 是列中的一些数据值teamName
吗?
我的数据库架构如下:
Teams(teamID,teamName,home,leagueName)
Games(gameID,homeTeamID,guestTeamID,date)
我如何查询(使用 SQL)teamID
团队与 team1 但不与 team2 对抗的所有 s。'team1 和 team2 是列中的一些数据值teamName
吗?
球队与 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
假设没有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'
)