title: PostGIS: Calculating minimum Distance between a Point and 2 fks: origin and destination
body: <p>I have a table <code>Transport</code> that has 2 fks pointing a table <code>Spot</code>. those fks store <code>origin</code> and <code>destination</code> of the transport.</p>
  
  <p>I need to do a query that gets the minimum distance between a point and origin and between the same point and destination.</p>
  
  <p>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:</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>Obviously I shouldn't be using an OR here. I have tried to do double JOINs, but didn't get it right. </p>
  
  <p>Thanks for your help</p>