我是一个SQL SELECT语句我需要提取两个团队的名称,从同一个表中获取两个团队。例如
SELECT sport_activity_id, (team A), (team B), date, time
FROM sportactivity, teams
WHERE competition_id_fk = 2
对于(团队A)和(团队B)我有一个team_id,这是表'团队'的FK
是否可以通过SQL从这些表中获得以下结果? 1,巴塞罗那,阿森纳,01/01 / 2000,20:00
这两个表如下:
表格运动
sport_activity_id, home_team_fk, away_team_fk, competition_id_fk, date, time
(元组示例) - > 1,33,41,5,2010-04-04,05:40:00
表团队
team_id, team_name
(元组示例) - > 1,阿尔及利亚
答案 0 :(得分:3)
是的,你只需要两次加入团队:
SELECT sport_activity_id, T1.team_name, T2.team_name, date, time
FROM sportactivity
JOIN teams T1 ON home_team_fk = T1.team_id
JOIN teams T2 ON away_team_fk = T2.team_id
WHERE competition_id_fk = 2
答案 1 :(得分:1)
SELECT sport_activity_id, teamA.team_Name, teamB.team_Name, date, time
FROM sportactivity
INNER JOIN teams teamA ON sportactivity.home_team_fk = teamA.team_ID
INNER JOIN teams teamB ON sportactivity.away_team_fk = teamB.team_ID
WHERE competition_id_fk = 2