我有一张篮球比赛桌和一张这样的篮球队桌:
MATCHES:
ID | HOME_TEAM_ID | AWAY_TEAM_ID | SCORE_HOME | SCORE_AWAY
----------------------------------------------------------------
1 | 20 | 21 | 80 | 110
2 | 12 | 10 | 96 | 90
TEAMS:
ID | NAME
-------------------------
20 | BULLS
21 | KNICKS
给定一个比赛 ID,我想同时检索比分和球队名称。如何进行 JOIN 以从 team 表中检索两个团队名称?
我试过了:
SELECT *
FROM matches AS m
JOIN teams AS t1 ON t.id = m.home_team_id
JOIN teams AS t2 ON ti.id = m.away_team_id
WHERE m.id = 1
...但是这里第二个 JOIN 语句的结果似乎覆盖了第一个语句的结果,所以我只得到一个名字:
[id] => 1
[score_home] => 80
[score_away] => 110
[name] => KNICKS
我也试过:
SELECT *
FROM matches AS m
JOIN teams AS t ON (t.id = m.home_team_id OR t.id = m.away_team_id)
WHERE m.id = 1
...返回两个结果:
[id] => 1
[score_home] => 80
[score_away] => 110
[name] => BULLS
和
[id] => 1
[score_home] => 80
[score_away] => 110
[name] => KNICKS
我想做一个返回类似这样的查询
[id] => 1
[score_home] => 80
[score_away] => 110
[name_home_team] => BULLS
[name_home_team] => KNICKS
那可能吗?