7

桌子Trips

TripId_PK
StartLocationId_FK
EndLocationId_FK

桌子Locations

LocationId_PK
Name

如何将这两个表加入两次,以便获得如下数据集:

TripId_PK
StartLocationName
EndLocationName

提前致谢。

4

2 回答 2

8
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
于 2013-01-17T22:20:45.933 回答
3

你可以试试这个

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
于 2013-01-17T22:21:07.617 回答