I have a table Transport that has 2 fks pointing a table Spot. those fks store origin and destination of the transport.
I need to do a query that gets the minimum distance between a point and origin and between the same point and destination.
This is what I have done, though this gets me 34 results and it should be 17. I know I’m doing it wrong, but I can’t get the query right:
SELECT LEAST(
ST_Distance(ST_GeographyFromText('SRID=4326; POINT(-3 40)'), s.point),
ST_Distance(ST_GeographyFromText('SRID=4326; POINT(-3 40)'), s.point)
) FROM spot s RIGHT OUTER JOIN transport t
ON t.origin = s.id OR t.destination = s.id;
Obviously I shouldn’t be using an OR here. I have tried to do double JOINs, but didn’t get it right.
Thanks for your help
You have to use two join, because your query need a table Spot meaning Origin, and a table Spot meaning Destination.
Try like this :