如何使用一个Sql查询可视化两个外键

时间:2016-03-18 15:19:35

标签: sql sql-server database

你好我有两个foreigns键名称可视化的问题。我在database开发足球sql,这是relationship diagram

的一部分

enter image description here

这是我的Select Query

Select team.Name, comp.Name,comp.Stage,fixDate.Date,news.Description
FROM Fixtures fix
Join Competition comp 
On fix.CompetitionID = comp.ID
Join FixtureDate fixDate
On fix.FixtureDateID=fixDate.ID
Join News news
On news.ID = fix.NewsId
Join Teams team
On fix.AwayTeamID = team.ID;

问题是我知道如何可视化homeTeam名称和awayTeam名称。我试试

 On fix.AwayTeamID = team.ID AND fix.HomeTeamID = team.ID

但输出为空响应..

以下是只有一个团队名称的输出

enter image description here

但是我想展示和他的对手客场球队。 谢谢你的建议!

1 个答案:

答案 0 :(得分:1)

为此需要两个带表别名的连接。

Select team1.Name, team2.Name, comp.Name, comp.Stage, fixDate.Date, news.Description
FROM Fixtures fix
Join Competition comp 
On fix.CompetitionID = comp.ID
Join FixtureDate fixDate
On fix.FixtureDateID=fixDate.ID
Join News news
On news.ID = fix.NewsId
Join Teams team1
On fix.HomeTeamID = team1.ID
Join Teams team2
On fix.AwayTeamID = team2.ID;