sql - How to avoid multiple column in correlated sub query assignment in MySQL update -
i'm attempting assign closest location community based on community postcode , using haversine formula sql described here. need return single scalar value can't seem avoid having second calculated distance value needed determine closest location. help.
update community c join postcode p on p.id = c.postcode_id join ( select 100.0 radius, 111.045 distance_unit ) set c.location_id = ( select l.id, a.distance_unit * degrees(acos(cos(radians(p.latitude)) * cos(radians(l.latitude)) * cos(radians(p.longitude - l.longitude)) + sin(radians(p.latitude)) * sin(radians(l.latitude)))) distance location l l.latitude between p.latitude - (a.radius / a.distance_unit) , p.latitude + (a.radius / a.distance_unit) , l.longitude between p.longitude - (a.radius / (a.distance_unit * cos(radians(p.latitude)))) , p.longitude + (a.radius / (a.distance_unit * cos(radians(p.latitude)))) having distance <= a.radius order distance limit 1 )
using structure have, need move distance calculation where
, order by
clauses:
set c.location_id = ( select l.id location l l.latitude between p.latitude - (a.radius / a.distance_unit) , p.latitude + (a.radius / a.distance_unit) , l.longitude between p.longitude - (a.radius / (a.distance_unit * cos(radians(p.latitude)))) , p.longitude + (a.radius / (a.distance_unit * cos(radians(p.latitude)))) , a.distance_unit * degrees(acos(cos(radians(p.latitude)) * cos(radians(l.latitude)) * cos(radians(p.longitude - l.longitude)) + sin(radians(p.latitude)) * sin(radians(l.latitude)))) <= a.radius order a.distance_unit * degrees(acos(cos(radians(p.latitude)) * cos(radians(l.latitude)) * cos(radians(p.longitude - l.longitude)) + sin(radians(p.latitude)) * sin(radians(l.latitude)))) limit 1 )
Comments
Post a Comment