2013-01-17 18 views
5

Tabella Tripsunire due tabelle con più chiavi esterne

TripId_PK 
StartLocationId_FK 
EndLocationId_FK 

Tabella Locations

LocationId_PK 
Name 

Come posso unire i due tavolo due volte in modo che io possa avere un set di dati come:

TripId_PK 
StartLocationName 
EndLocationName 

Grazie in anticipo.

risposta

6
SELECT t.TripId_PK, ls.name StartLocationName, le.name EndLocationName 
FROM trips t 
JOIN locations ls 
ON  ls.LocationId_PK = t.StartLocationId_FK 
JOIN locations le 
ON  le.LocationId_PK = t.EndLocationId_FK 
2

Si può provare questo

SELECT t.TripId_PK, ls.StartLocationName, le.EndLocationName 
FROM Trips t 
JOIN Locations ls ON t.StartLocationId_FK = ls.LocationId_PK 
JOIN Locations le ON t.EndLocationId_FK = le.LocationId_PK 
Problemi correlati