Mysql查询 - JOIN语句

时间:2014-08-24 10:46:51

标签: mysql sql left-join inner-join

我需要查询两个表:

Fixtures
-----------
fixture_id
hometeam_id
awayteam_id
fixture_date


Teams
-----------
team_id
team_name

我需要从hometeam_idawayteam_id中提取团队名称,我可以使用左边或内部连接语句来获取一个,但无法在不使用子语句。

SELECT t.team_name,f.away_teamid,f.home_teamid 
FROM fixtures f
LEFT JOIN teams t 
ON (t.team_id = f.hometeam_id)

总结一下,我需要每个灯具中的两个团队名称

提前致谢

2 个答案:

答案 0 :(得分:0)

加入球队表两次以获得主队和客队的名称,如果你的组合表必须包含主队和客队的关联,则使用内联,但如果主场或客场可以有空,那么使用左连接< / p>

SELECT 
f.fixture_id,
t.team_name home_team,
t1.team_name away_team,
f.fixture_date
from fixtures f 
join teams t on (t.team_id = f.hometeam_id)
join teams t1 on (t1.team_id = f.away_teamid)

答案 1 :(得分:0)

SELECT home.team_name AS Home, away.team_name AS Away FROM Fixtures fix
INNER JOIN Teams away on away.team_id = fix.awayteam_id
INNER JOIN Teams home on home.team_id = fix.hometeam_id