This is not a duplicate of: Find distance between two points using latitude and longitude in mysql
I tried the query and it's not working, I get empty data set and 0 as the answer.
SEE answer, THAT is the solution, I don't know why this question was marked as duplicate from Shadow
i have a table with the following structure:
table: distance
id int(10) primary key
lat float,
lon float,
time timestamp
i have a android application that ping my server and add a record each time it change position.
for example
id:1, lat:40.753979, lon:-111.881721, time = 1571004620
id:2, lat:40.753979, lon:-111.883721, time = 1571004630
id:3, lat:40.753979, lon:-111.885721, time = 1571004640
I can calculate the total distance if I go one direction:
/* in my python script*/
startLat,startLon = select lat,lon from distance where time >= 1571004620 order by time limit 1;
endLat,endLon = select lat,lon from distance where time <= 1571004640 order by time desc limit limit 1;
then i can substract both coordinates, which end up giving me a longitute distance of 0.004000
problem:
if i add:
id:4, lat:40.753979, lon:-111.881721, time = 1571004650
then I should get: 0.008000
but i get 0 since position 1 is the same as position 4
You can take advantage of MySQL spatial support, available since 5.7 to perfom the whole computation within the database. Function
ST_Distance_Sphere()
can be used to compute distances.You actually need a cumulative sum of the distance (this requires window functions, available since MySQL 8.0).
Consider:
Demo on DB Fiddle:
In earlier versions of MySQL, you need to emulate window functions:
LAG()
can be replaced with a self-LEFT JOIN
with aNOT EXISTS
condition and a correlated subquery in theON
conditionvariables can emulate the cumulative
SUM
Query:
Demo on DB Fiddle: same results as above.