join - MySQL, using a joined table twice in the same query -


i'm trying write mysql query joins 2 columns in same table table. have looked through many examples , tried great many things, of them error 1 reason or another.

i have teams table:

 id | team  1  | team 1  2  | team 2  3  | team 3  4  | team 4 

i have games table:

 id | team_1 | team_2   1  |  1     | 2   2  |  2     | 3   3  |  3     | 4   4  |  4     | 1 

i have following query:

select pio.games.id       , team.name `team-1`       , team.name `team-2`   games   left    join team      on games.team_1 = team.id   left    join team t2      on games.team_2 = team.id 

i need list games , show team names, expect:

game_id   team-1     team-2   1    |     team 1  |   team 2   2    |     team 2  |   team 3   3    |     team 3  |   team 4   4    |     team 4  |   team 1 

but i'm getting first team twice:

 game_id |  team-1  |   team-2  1       |  team 1  |   team 1  2       |  team 2  |   team 2  3       |  team 3  |   team 3  4       |  team 4  |   team 4 

i've spent several hours far on , have come nothing works! assume it's simple i'm missing.

i have tried solutions on mysql database in mamp in both navicat , sequelpro.

anyone ideas? gratefully appreciated.

use aliases created. allow join team table twice won't confuse db engine.

    select g.id, t.name team-1, t2.name team-2     games g     left join teams t on g.team_1 = t.id     left join teams t2 on g.team_2 = t2.id 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -