I have a table called user, and I need to get another users in an expecific distance range and the distance between them. My query is this:
select ( 6371 * acos(
cos( radians(users.latitude) )
* cos( radians(-22.9035) )
* cos( radians(-43.2096) - radians(users.longitude) )
+ sin( radians(users.latitude) )
* sin( radians(-22.9035) )
)
) as distance, users from users where users.id != 41 and distance > 50
I need to recover the user list and the distance, but the "as distance" is not working:
ERROR: column "distance" does not exist.
I tried with:
with distance as (
select ( 6371 * acos(
cos( radians(users.latitude) )
* cos( radians(-22.9035) )
* cos( radians(-43.2096) - radians(users.longitude) )
+ sin( radians(users.latitude) )
* sin( radians(-22.9035) )
)
) from users
)
select * from users where users.id != 41 and distance > 50
However, the error remains the same.