SELECT
zips.id, zips.latitude, zips.longitude, cities.name,
69 *
DEGREES(
ACOS(
COS(RADIANS(latpoint)) * COS(RADIANS(zips.latitude)) * COS(RADIANS(longpoint) - RADIANS(longitude)) +
SIN(RADIANS(latpoint)) * SIN(RADIANS(latitude))
)
) AS distance_in_mi
FROM zips LEFT JOIN cities ON zips.city_id = cities.id JOIN ( SELECT 42.81 AS latpoint, -70.81 AS longpoint, 50.0 AS r) AS p
WHERE
latitude BETWEEN
latpoint - (r / 69)
AND
latpoint + (r / 69)
AND
longitude BETWEEN
longpoint - (r / (69 * COS(RADIANS(latpoint))))
AND
longpoint + (r / (69 * COS(RADIANS(latpoint))))
AND
distance_in_mi <= r
ORDER BY distance_in_mi
Unknown column 'distance_in_mi' in 'where clause'
В то же время distance_in_mi работает в ORDER BY но не в WHERE. Почему? Как правильно?