D
D
dedulka2019-06-08 22:50:08
MySQL
dedulka, 2019-06-08 22:50:08

How to select the distance and id of a geopoint?

Good afternoon.
There are two tables.
geo
id,name,latitude,longitude
geotmp
id,idtmp,latitude,longitude,tmpdist,tmpid
to find the nearest point from geotmp to geo do

UPDATE geotmp SET tmpdist=(SELECT (6371 * 
acos(cos(RADIANS(geotmp.latitude)) *  cos(radians(latitude)) *  cos(radians(longitude) -  RADIANS(geotmp.longitude)) +  
sin(RADIANS(geotmp.latitude)) *   sin(radians(latitude))))   AS distance FROM geo  HAVING DISTANCE < 0.5 ORDER BY distance ASC LIMIT 1);

Those. I update the distance in geotmp with a request.
And how to add the id of the found point to this request
. add
,tmpid=geo.id
something to not make a second request.
Now in php I do this:
SELECT *, 
(6371 * 
acos( 
cos(radians(".$latitude.")) * 
cos(radians(latitude)) * 
cos(radians(longitude) - 
radians(".$longitude.")) + 
sin(radians(".$latitude.")) * 
sin(radians(latitude)))) 
AS distance FROM geo 
HAVING distance <= 1 ORDER BY distance ASC limit 1

How can it be combined into one request?
Thank you!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question