0

i have DataBase function that calculate distance by coordinates

CREATE OR REPLACE FUNCTION distance(lat1 FLOAT, lon1 FLOAT, lat2 FLOAT, lon2 FLOAT) RETURNS FLOAT AS $$
DECLARE                                                   
    x float = 69.1 * (lat2 - lat1);                           
    y float = 69.1 * (lon2 - lon1) * cos(lat1 / 57.3);        
BEGIN                                                     
    RETURN sqrt(x * x + y * y);                               
END  
$$ LANGUAGE plpgsql;

now i have 2 tables (with one to one relation) one called

person with columns --> Personid, name, lastname

and one

location with columns --> Personid,latitude,longtitude.

now i try to get the person that the distance is less to 5 using the distnace function

i start with this following query to get the id first

select loc.id,loc.latitude,loc.longitude from location loc
where distance(123,456,loc.latitude,loc.longitude)<0.5

but don't know how to get the all persons with the id from the above query.

how can i do that? thanks in advance.

1 Answer 1

2

Is this what your are looking for?

select p.*, loc.id,loc.latitude,loc.longitude 
from location loc inner join person p on (p.Personid=loc.Personid)
where distance(123,456,loc.latitude,loc.longitude)<0.5
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.