For caching reasons I need to write the Km (kilometers) field back to the location table, so far I was able to calculate the Km. Everything I am trying so far (update with unions, subqueries, etc) gives me the SQL Error 1093.
In MySQL,how do I update the Km value back to the table?
This is the table schema
CREATE TABLE `locations` (
`epoch_date` int(10) unsigned DEFAULT NULL,
`latitude` float NOT NULL,
`longitude` float NOT NULL,
`km` float DEFAULT NULL
);
INSERT INTO `locations` (`epoch_date`, `latitude`, `longitude`, `km`) VALUES
(1429913506, -8.7285, 119.401, NULL),
(1429913631, -9.1279, 117.67, NULL),
(1429945707, -8.7063, 119.36, NULL),
(1431929523, -8.5745, 119.707, NULL),
(1431941343, -8.5773, 119.713, NULL),
(1431958383, -8.5881, 119.724, NULL),
(1431969963, -8.589, 119.728, NULL),
(1431998403, -8.5766, 119.724, NULL);
This is the select query that generates the KM (kilometer)
SELECT
latitude
, longitude
, epoch_date
, @latitude2 :=
(
SELECT
latitude
FROM locations loc1
WHERE
loc1.epoch_date < loc.epoch_date
ORDER BY epoch_date DESC LIMIT 1 OFFSET 0
) as prev_latitude
, @longitude2 :=
(
SELECT
longitude
FROM locations loc1
WHERE
loc1.epoch_date < loc.epoch_date
ORDER BY epoch_date DESC LIMIT 1 OFFSET 0
) as prev_longitude
, (ACOS(COS(RADIANS(90-latitude)) *COS(RADIANS(90-@latitude2)) +SIN(RADIANS(90-latitude)) *SIN(RADIANS(90-@latitude2)) *COS(RADIANS(longitude-@longitude2))) *6371) as km
FROM locations loc
ORDER BY epoch_date
Here is a link to the SQL Fiddle http://sqlfiddle.com/#!9/7f95de/2/0