I am trying to run a query that finds the places near my location, but every time I try to run it, it returns the following error:
column "distance" does not exist
If I remove distance and only leave until FROM posts
it returns the post id and a column distance
.
But if I leave it in the original way, it returns the error.
SELECT id, ( 3959 * acos( cos( radians(-32.63) ) * cos( radians( latitude ) ) *
cos( radians( longitude ) - radians(-71.42) ) + sin( radians(-32.63) ) *
sin( radians( latitude ) ) ) ) AS distance FROM posts HAVING
distance < 25 ORDER BY distance;
This is happening because aliased columns can be used in an order by but they cannot be evaluated by a where, group by or having you can see it in the sql server documentation sql server documentation
A possible solution could be this:
Be careful because this code thus becomes a bit more complicated to read