I'm trying to use the longitude and latitude data in one of my tables to find the timezone for each entry in a table.
One of my tables, let's call it Table1 has the format below
latitude | longitude
12.12232 | 12.122322
23.23232 | 23.232323
I have been able to populate a table with each timezone and their latitude/longitude data. This table is called destination.
I now need to create and populate a column in Table1 to display the closest timezone for each row of data. The link here uses static SET @my_lat = 35.229205 ; -- the source latitude SET @my_lon = -114.26811 ;-- the source longitude data to calculate the closest timezone. I would like to do this using a dynamic query. My approach thus far has been
-- Add column to Table1
ALTER TABLE Table1
MODIFY timezone_fixed VARCHAR(200);
-- Insert data into Table1's timezone_fixed column
INSERT INTO Table1 (timezone_fixed)
SELECT timezone FROM
(SELECT *, ( 3959 * acos( cos( radians(
tbl.latitude) ) * cos( radians(
destination.latitude ) ) * cos( radians(
destination.longitude ) - radians(
tbl.longitude) ) + sin( radians(
tbl.latitude) ) * sin( radians(
destination.latitude ) ) ) ) AS distance
FROM destination ds
-- destination is a table that contains latitude, longitude and timezone for each timezone. This was populated in the previous step.
JOIN (SELECT longitude, latitude FROM Table1) tbl
ORDER BY distance LIMIT 1);
I have a feeling that this won't populate the closest timezone for each row in Table1 but the closest one to any of the rows in Table1. Also, I'm not sure if this is the most efficient way to find the closest timezone for each entry. Any help and tips would be appreciated.
Aucun commentaire:
Enregistrer un commentaire