简体   繁体   English

MySQL内部连接别名问题

[英]MySQL inner join alias issue

I am trying to write an sql join but I'm getting a not unique error: 我正在尝试编写sql联接,但出现了一个不唯一的错误:

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 - Not unique table/alias: 'teamsh'. 1066-不是唯一的表格/别名:“ teamsh”。

I know i'm close but the alias has beaten me. 我知道我已经接近了,但是别名打败了我。

Thank you in advance, 先感谢您,

Al.

You're mixing old-style joins with proper join syntax: 您正在混合使用适当的联接语法的老式联接:

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 

You were getting the error because teamsh and teamsa were used multiple times as table names/aliases. 您收到错误消息是因为teamshteamsa多次用作表名/别名。

The problem is in your FROM clause. 问题出在您的FROM子句中。 It should look like this: 它看起来应该像这样:

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 ​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM