Does anyone know how I can do an inner joins and alias values within so they won’t overwrite each other? It might look more clear if you see my code:
SELECT home, away, g.network, g.date_start FROM game g INNER JOIN team t ON ( (t.importid = g.home) as home OR (t.importid = g.away) as away ) ORDER BY date_start DESC LIMIT 7
SOLVED (After the help below here is my final query)
SELECT home.market AS home_market, away.market AS away_market, g.network, g.date_start FROM game AS g INNER JOIN team AS home ON ( home.importid = g.home ) INNER JOIN team AS away ON ( away.importid = g.away ) ORDER BY g.date_start DESC LIMIT 7
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You’ll need to join twice:
SELECT home.*, away.*, g.network, g.date_start FROM game AS g INNER JOIN team AS home ON home.importid = g.home INNER JOIN team AS away ON away.importid = g.away ORDER BY g.date_start DESC LIMIT 7
Method 2
Use a seperate column to indicate the join condition
SELECT t.importid, case when t.importid = g.home then 'home' else 'away' end as join_condition, g.network, g.date_start FROM game g INNER JOIN team t ON (t.importid = g.home OR t.importid = g.away) ORDER BY date_start DESC LIMIT 7
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0