SqlFiddle
非常基本,但您仍然可以看到这种关系。在这里。
结构
表
球员
--------------------------
|Id | Name | OtherInfo |
--------------------------
| 1 | John | Player Info |
--------------------------
| 2 | Tom | Player Info |
--------------------------
四季
-----------------------------------
|Id | SeasonName | SeasonInfo |
-----------------------------------
|01 | MyFirstSeason | Season Info |
-----------------------------------
比赛
------------------------------------
| Id | SeasonId | TournamentName |
------------------------------------
|001 | 01 | MyFirstTournament|
------------------------------------
邂逅
---------------------------------------
| Id | FirstPlayerId | SecondPlayerId |
---------------------------------------
| A | 1 | 2 |
---------------------------------------
比赛
-----------------------------------------------
| Id | TournamentId | EncounterId | MatchInfo |
-----------------------------------------------
|0001| 001 | A | Some Info |
-----------------------------------------------
事情是你无用地使用了很多键。你只需要退后一步考虑一下设计。我知道我删除了一些专栏,但这只是为了举例。我相信你知道把它们放回去的地方。我只是想展示这些表之间的关系。
询问
让我们暗示您只需要基本信息(再次仅用于示例)
Select m.Id,
t.TournamentName,
s.SeasonName,
(SELECT pl.Name FROM Players pl
JOIN Encounters en ON pl.Id = en.FirstPlayerId
WHERE pl.Id = en.FirstPlayerId) AS [FirstPlayerName],
(SELECT pl.Name FROM Players pl
JOIN Encounters en ON pl.Id = en.SecondPlayerId
WHERE pl.Id = en.SecondPlayerId) As [SecondPlayerName],
m.MatchInfo
FROM Matchs m
JOIN Tournaments t ON m.TournamentId = t.Id
JOIN Encounters e ON m.EncounterId = e.Id
JOIN Seasons s ON t.SeasonId = s.Id