I'm looking to select the distance from a given location to the nearest location in a joining table, and order by it. In my use case I have a listings table which is joined to locations table through listing_location. The locations table has two columns for coordinate - latitude and longitude (both doubles).
SELECT
listings.*
ST_Distance(ST_Point(?, ?), ST_ClosestPoint(:unsure:, ST_Point(?, ?)), FALSE) AS distance)
JOIN listing_location
ON listing_location.listing_id = listings.id
JOIN locations
ON locations.id = listing_location.location_id
ORDER BY distance ASC
The question marks would be bound to the longitude and latitude of the given location, but I'm unsure what to pass into :unsure: in order to select the joined locations. Am I missing something, or thinking about this problem in the wrong way? Is this problem solvable when my location data is stored as latitude/longitude double columns?