MySQL内连接别名问题

时间:2015-02-02 16:33:55

标签: mysql join alias

我正在尝试编写一个sql join,但我收到了一个不唯一的错误:

SELECT matches.match_id, 
       teamsh.team_name  AS "homeTeam", 
       teamsh.team_id    AS "homeID", 
       teamsa.team_name  AS "awayTeam", 
       teamsa.team_id    AS "awayID", 
       competition.NAME, 
       competition.competition_id, 
       teamsh.stadium, 
       matches.date, 
       teamsh.name_short AS "homeTeamShort", 
       teamsa.name_short AS "awayTeamShort", 
       teamsh.pysioid    AS "pysioIDh", 
       teamsa.pysioid    AS "pysioIDa" 
FROM   matches, 
       teams teamsh, 
       teams teamsa, 
       competition

INNER JOIN
teamsh ON matches.home_team_id = teamh.team_id,
teamsa ON matches.away_team_id = teama.team_id,
competition ON matches.competition_id = competition.competition_id ​

WHERE  match_id = 22268 

1066 - 不是唯一的表/别名:' teamsh'。

我知道我已经关闭了但别名已经打败了我。

提前谢谢你,

的Al。

2 个答案:

答案 0 :(得分:2)

您正在使用正确的连接语法混合旧式连接:

SELECT matches.match_id, 
       teamsh.team_name  AS "homeTeam", 
       teamsh.team_id    AS "homeID", 
       teamsa.team_name  AS "awayTeam", 
       teamsa.team_id    AS "awayID", 
       competition.NAME, 
       competition.competition_id, 
       teamsh.stadium, 
       matches.date, 
       teamsh.name_short AS "homeTeamShort", 
       teamsa.name_short AS "awayTeamShort", 
       teamsh.pysioid    AS "pysioIDh", 
       teamsa.pysioid    AS "pysioIDa" 
FROM   matches
INNER JOIN
teams AS teamsh ON matches.home_team_id = teamsh.team_id
INNER JOIN 
teams AS teamsa ON matches.away_team_id = teamsa.team_id
INNER JOIN
competition ON matches.competition_id = competition.competition_id ​
WHERE  match_id = 22268 

您收到错误是因为teamshteamsa被多次用作表名/别名。

答案 1 :(得分:1)

问题出在你的FROM条款中。它应该是这样的:

FROM  matches
INNER JOIN
teams teamsh ON matches.home_team_id = teamh.team_id
INNER JOIN
teams teamsa ON matches.away_team_id = teama.team_id
INNER JOIN
competition ON matches.competition_id = competition.competition_id ​