This SQLFiddle example describes 2 tables and their relationship:
Primary Routes: A direct route between 2 places. Indirect primary routes are for relationship purposes with the secondary routes table
Secondary Routes: A route between 2 places where no direct primary route exists
Now, a user wants to go from one place to another. So, for this example, a user selects the following points:
- London->Harlow:
A direct route exists. The SQL is simple:
SELECT *
FROM primary_routes
WHERE
(
(point1 = 'London' AND point2 = 'Harlow')
OR (point1 = 'Harlow' AND point2 = 'London')
)
AND direct = 1
A route is only entered once in the DB, however a route goes both ways.
- Stanmore->Waltham:
No direct route exists, however both these points lie on the same route. The SQL is:
SELECT DISTINCT primary_id
FROM secondary_routes
WHERE point IN ( 'Stanmore', 'Waltham')
Now, the complexity will increase because there might be other kinds of connections, for example:
London-Sheering: No route from 1 and 2 above fits. However, routes exist between London->Harlow and Harlow-Sheering.
Wembley-Shenley: No route from 1, 2, or 3 fits. However, routes exist between Wembley->London->Watford->Shenley, or Wembley->London->Harlow->Shenley
Is it possible to build a (not so complex) SQL statement that will return the routes for 3 and 4, and furthermore, for each route found (including in 2), the distance between the 2 points must be calculated and be part of the route.