Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL INNER JOIN Alias

Tags:

select

mysql

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 
like image 480
JREAM Avatar asked May 23 '12 16:05

JREAM


People also ask

Can you use alias in join SQL?

SQL aliases are custom names that you can give to the columns and tables you include in your queries. Aliases are very useful, as they can greatly improve the readability and maintainability of your query.

Can you use column alias in join?

No, you cannot do that. The alias is not bound until later in the processing. You can use "Nombre" in an ORDER BY, but not in a WHERE clause and certainly not in a JOIN clause.

Is join the same AS inner join MySQL?

Difference between JOIN and INNER JOINJOIN returns all rows from tables where the key record of one table is equal to the key records of another table. The INNER JOIN selects all rows from both participating tables as long as there is a match between the columns.


1 Answers

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 
like image 163
Marcus Adams Avatar answered Oct 04 '22 13:10

Marcus Adams