Problems with Sub Query

I am having problems with the way I read about coding. I have one table with a list of lat / longitudes and another table with a list of place names with latitudes and longitudes. What I need to do is: 1) Loop through each item in table 1, take lat and long, search by radius on table 2 and take the place name, update table 1 with the new place name. This code gets me the name of the place I need:

$sql="SELECT *, (3959 * acos(cos(radians('".$lat."')) * cos(radians(lat)) * cos( radians(lon) - radians('".$lng."')) + sin(radians('".$lat."')) * 
sin(radians(lat)))) 
AS distance 
FROM cats HAVING distance < 5 ORDER BY distance LIMIT 1";

      

I need help figuring out how I am joining the two queries together, or the best way for me to go through the lat / longs from table 1. I tried to do it with a php loop but I don't think it is the best way and I couldn't get it to work

Thanks for any help or suggestions!

+3


source to share


1 answer


Basis of your description I think you want something like this:



UPDATE table1 SET place_name = (SELECT place_name FROM 
table2
WHERE (3959 * acos(cos(radians('".$lat."')) * cos(radians(lat)) * cos( radians(lon) - radians('".$lng."')) + sin(radians('".$lat."')) * 
sin(radians(lat)))) < 5
ORDER BY (3959 * acos(cos(radians('".$lat."')) * cos(radians(lat)) * cos( radians(lon) - radians('".$lng."')) + sin(radians('".$lat."')) * 
sin(radians(lat)))) DESC
LIMIT 1)

      

+1


source







All Articles