Hi all, I've got a problem regarding joins, specifically joining a one table to another twice in the same query. This is MySQL if it matters. Tables something like this:
Code:
TABLE:Locations
.....ID...Location
.....1....Home
.....2....Gym
.....3....Work
.....4....Post Office
TABLE:ROUTES
.....ID....StartID....EndID.....Distance....Via
.....1........1.........2..........4.5......Elmo's
.....2........1.........3..........1.2......Direct
.....3........3.........4..........0.5......Clockwise
I want a query that will give me the name of the start and end location, and the distance, for each route. I've tried:
Code:
SELECT Locations.Location AS Start, Locations.Location AS End, Routes.Distance AS Distance FROM Locations JOIN Routes ON Locations.ID=Routes.StartID JOIN Locations ON Routes.EndID=Locations.ID
MySQL doesn't like that, understandably, but I can't see what to do. I'm assuming that it can be done.
Possibly important notes:
- There will never be a route from a location to the same location
- Not all possible routes will be completed
- Some routes will have the reverse completed with a different distance (one way streets etc)
- Some routes will be present twice, with different values for Via
Many thanks,
Tim
Bookmarks