Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jahabdank
Starting Member
2 Posts |
Posted - 2007-09-16 : 07:04:28
|
| Hello Everyone,I have a quite unusual problem, and I have hard time finding the answer.I have a table with Locations - lets say that it has just ID, and Name,and a Transport table containing the ID, ArrivalLocationID and DepartureLocationID.Now - when I select the Transport table I want to get names of the Arrival and Departure locations from th other table.If it was a single link I woul do an INNER JOIN like:SELECT Transport.*,Locations.Name AS ArrivalLocationFROM TransportProductOperationPeriodsINNER JOIN Locations ON Transport.ArrivalLocation = Locations.IDBut I want to do a double INNER JOIN between two same table. And here I get a problem - how to do it? Something like:SELECT TransportProductOperationPeriods.*,Locations.Name AS LArrivalLocation,Locations.Name AS LDepartureLocation,Locations.ID AS LArrivalLocationID,Locations.ID AS LDepartureLocationIDFROM TransportProductOperationPeriodsINNER JOIN Locations ON TransportProductOperationPeriods.ArrivalLocation = LArrivalLocationID INNER JOIN Locations ON TransportProductOperationPeriods.DepartureLocation = LDepartureLocationIDbut obviously that does not work ;)Thanks in advance,Joseph |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-09-16 : 07:38:54
|
You need to provide Alias names for your tables, so that references to them are un-ambigous:SELECT TransportProductOperationPeriods.*,LA.Name AS LArrivalLocation,LD.Name AS LDepartureLocation,LA.ID AS LArrivalLocationID,LD.ID AS LDepartureLocationIDFROM TransportProductOperationPeriodsINNER JOIN Locations AS LAON LA.LArrivalLocationID = TransportProductOperationPeriods.ArrivalLocation INNER JOIN Locations AS LDON LD.LArrivalLocationID = TransportProductOperationPeriods.DepartureLocation Kristen |
 |
|
|
jahabdank
Starting Member
2 Posts |
Posted - 2007-09-16 : 11:26:40
|
| Thanks a *lot*. That was exactly what I needed,Joseph |
 |
|
|
|
|
|
|
|