2011-03-31 3 views
2

J'ai ces deux tablesrequête mysql - 2 clés étrangères

airports: airportid, airportname 
flights: flightid, from_airport, to_airport 

from_airport et to_airport sont les clés étrangères.

Je peux joindre des tables soit sur airportid et from_airport ou airportid et to_airport et soit je reçois le nom de to_airport ou le nom de from_airport mais je veux sélectionner les deux to_airport et from_airport noms soit dans une requête ou à un coût minimal.

Est-ce possible? Comment??

Voici ma question:

SELECT 
flight.idflight, 
flight.idairline, 
flight.from_airport, 
flight.to_airport, 
flight.number, 
airports.name AS origin 
FROM 
flight 
Inner Join airports ON flight.from_airport = airports.idairports 

Répondre

2

Alias ​​vos tables lorsque vous faites les jointures:

SELECT 
    flight.idflight, 
    flight.idairline, 
    flight.from_airport, 
    flight.to_airport, 
    flight.number, 
    airport_from.name AS origin 
    airport_to.name AS destination 
FROM flight 
    INNER JOIN airports airport_from ON flight.from_airport = airport_from.idairports 
    INNER JOIN airports airport_to ON flight.to_airport = airport_to.idairports 
Questions connexes