I am trying to find the time difference ad distance between two lat long pairs.
I am getting incorrect values of time for a few records
SELECT A.ID, A.latitude, A.longitude, A.drive_time, (B.drive_time - A.drive_time) AS time_difference, 6367 * acos( cos(
radians(A.latitude) )
* cos( radians(B.latitude) )
* cos( radians(B.longitude) - radians(A.longitude)) + sin(radians(A.latitude))
* sin( radians(B.latitude) ) as distance
FROM mapper A INNER JOIN mapper B ON B.ID = (A.ID + 1);
Never mind I found my solution.
SELECT A.ID, A.latitude, A.longitude, B.drive_time, 6371000 * 2 * ASIN(SQRT(
POWER(SIN((A.latitude - abs(B.latitude)) * pi()/180 / 2),
2) + COS(A.latitude * pi()/180 ) * COS(abs(B.latitude) *
pi()/180) * POWER(SIN((A.longitude - B.longitude) *
pi()/180 / 2), 2) )) as distance ,time_to_sec(TIMEDIFF(B.drive_time,A.drive_time)) AS time_difference
FROM mapper A left JOIN mapper B ON B.ID = A.ID + 1
Related
I really wanna know why place B is more far than place C to place A, when i use Haversine formula, but when i see on the map the place C is more distant, i will show some data:
Place A - lat: -23.598593 lon:-46.644701
Place B - lat: -23.575966 lon:-46.619133 to A distance = 3.6117730949107836
Place C - lat: -23.594992 lon:-46.636688 to A distance = 0.9068309093558656
I'm using this query :
QUERY A to B
set #latdest = -23.575966;
set #londest = -46.619133;
set #lat = -23.598593;
set #lon = -46.644701;
select 6353 * 2 * ASIN(SQRT(POWER(SIN((#lat - #latdest) * pi()/180 / 2), 2)
+ COS(#lat * pi()/180 ) * COS(#latdest * pi()/180)
* POWER(SIN((#lon - #londest) * pi()/180 / 2), 2) )) as distance
QUERY A to C
set #latdest = -23.594992;
set #londest = -46.636688;
set #lat = -23.598593;
set #lon = -46.644701;
select 6353 * 2 * ASIN(SQRT(POWER(SIN((#lat - #latdest) * pi()/180 / 2), 2)
+ COS(#lat * pi()/180 ) * COS(#latdest * pi()/180)
* POWER(SIN((#lon - #londest) * pi()/180 / 2), 2) )) as distance
this is Lat and Lon example in mysql database
ID | Lat | Lon
1 | 3.1412 | 101.6865
2 | 1.2897 | 103.8501
The formula below calculates the distance between two Lat (latitudes) and Lon (longitudes) and gets the result that I want. But this formula doesn't extract my Lat and Lon from the database. I would like to ask, how to get the distance result by using ID and calculate two Lat and Lon? My friend told me using Join table, but I still don't get the answer..
SELECT ROUND(6353 * 2 * ASIN(SQRT( POWER(SIN((3.1412 -
abs(1.2897)) * pi()/180 / 2),2) + COS(3.1412 * pi()/180 ) * COS(
abs(1.2897) * pi()/180) * POWER(SIN((101.6865 - 103.8501) * pi()/180 / 2), 2) )), 2);
Thanks for advance
You can join the same table two time one for Id 1 and one for id 2 assigning different alias name
then use the related column name
SELECT ROUND(6353 * 2 * ASIN(SQRT( POWER(SIN((a.lat -
abs(b.lat)) * pi()/180 / 2),2) + COS(a.lat * pi()/180 ) * COS(
abs(b.lat) * pi()/180) * POWER(SIN((a.lng - b.lng) * pi()/180 / 2), 2) )), 2);
from my_table a
inner join my_table b on a.id = 1 and b.id = 2
It is working for me
SELECT *,(((acos(sin((12.942999*pi()/180)) * sin((`Lat`*pi()/180))+cos((12.942999*pi()/180)) * cos((`Lat`*pi()/180)) * cos(((74.82994- `Lon`)* pi()/180))))*180/pi())*60*1.1515*1.609344 ) as distance FROM ven_vendor HAVING distance <= 10
My code is as follows and should work but for some reason it is not recognizing wm_stores.lat in the join statement. Anyone have any ideas?
SELECT wm_dcs.dc_id, wm_stores.store_id
FROM wm_dcs
JOIN wm_stores
ON wm_stores.lat BETWEEN wm_dcs.lat - (250.0 / 69.0)
AND wm_dcs.lat + (250.0 / 69.0)
AND wm_stores.lon BETWEEN wm_dcs.lon - (250.0 / (69.0 * COS(RADIANS(wm_dcs.lat))))
AND wm_dcs.lon + (250.0 / (69.0 * COS(RADIANS(wm_dcs.lat))))
AND (69.0 * DEGREES(ACOS(COS(RADIANS(wm_dcs.lat) * COS(RADIANS(stores.latitude))
* COS(RADIANS(dc.longitude - stores.longitude))
+ SIN(RADIANS(dc.latitude))
* SIN(RADIANS(wm_stores.lon)))))) <= 250.0;
Different Version of code:
set #dc_lat = 40.811973 ;
set #dc_lon = -73.946299 ;
select wm_stores.store_id,
( 3959 * acos( cos( radians(#dc_lat) ) * cos( radians(wm_stores.lat ) )
* cos( radians( wm_stores.lon ) - radians(#dc_lon) ) + sin( radians(#dc_lat) )
* sin( radians( wm_stores.lat ) ) ) ) AS distance
from wm_stores
having distance <= 250
order by distance asc;
In your query
SELECT wm_dcs.dc_id, wm_stores.store_id
FROM wm_dcs
JOIN wm_stores
ON wm_stores.lat BETWEEN wm_dcs.lat - (250.0 / 69.0)
AND wm_dcs.lat + (250.0 / 69.0)
AND wm_stores.lon BETWEEN wm_dcs.lon - (250.0 / (69.0 * COS(RADIANS(wm_dcs.lat))))
AND wm_dcs.lon + (250.0 / (69.0 * COS(RADIANS(wm_dcs.lat))))
AND (69.0 * DEGREES(ACOS(COS(RADIANS(wm_dcs.lat) * COS(RADIANS(stores.latitude))
* COS(RADIANS(dc.longitude - stores.longitude))
+ SIN(RADIANS(dc.latitude))
* SIN(RADIANS(wm_stores.lon)))))) <= 250.0;
I noticed there is no table like stores but you have used stores.latitude and stores.longitude in your query. May be this is causing the error message Unknown column 'stores.latitude' in 'on clause'.
I suggest you to re-examine the code
AND (69.0 * DEGREES(ACOS(COS(RADIANS(wm_dcs.lat) * COS(RADIANS(stores.latitude))
* COS(RADIANS(dc.longitude - stores.longitude))
+ SIN(RADIANS(dc.latitude))
* SIN(RADIANS(wm_stores.lon)))))) <= 250.0;
I'm trying to select events in a 5 km radius and order them by likes and from oldest to newest. I know how to select events and order them by likes, but I can't figure this one out. The problem maybe is that the location is stored in a different table.
Database structure:
event plusone (like) location
id_event id_plusone id_location
location_id event_id lat
... user_id lon
... city_id
full_name
....
The SQL statement I'm trying:
SELECT 3956 * 2 * ASIN(
SQRT(POWER(SIN((50.127946 - abs(l.lat)) * pi()/180 / 2),2) + COS(50.127946 * pi()/180 ) *
COS(abs(l.lat) * pi()/180) * POWER(SIN((14.4882912 - l.lon) * pi()/180 / 2), 2) )) AS distance,e.*,COUNT( p.id_plusone ) AS
likes
FROM
event e,location l
LEFT JOIN plusone p ON p.event_id = e.id_event
WHERE
e.location_id = l.id_location AND distance<=5 GROUP BY e.id_event
ORDER BY e.timestamp DESC,likes DESC
The huge column at the beginning is calculation of distance of the event from user position using latitude and longitude.
This is what I get from the SQL debugger:
#1054 - Unknown column 'distance' in 'where clause'
Does anybody know how to do this?
Is hard to track where the error is, you should try to solve the distance part first.
SELECT
event_id,
3956 * 2 * ASIN(SQRT( POWER(SIN((50.127946 - abs(l.lat)) * pi()/180 / 2),2)
+ COS(50.127946 * pi()/180 ) * COS(abs(l.lat) * pi()/180) * POWER(SIN((14.4882912 – l.lon) * pi()/180 / 2), 2) )) AS distance,
FROM event e
JOIN location l
WHERE distance <= 5
And then join to back to the event table
SELECT e.*, count(p.id_plusone)
FROM event e
LEFT JOIN plusone p
ON p.event_id = e.id_event
JOIN ( Previous Distance Query) d
ON e.event_id = d.event_id
GROUP BY e.id_event
ORDER BY e.timestamp DESC, likes DESC
and btw, this group_by will bring you event_id plus random values for e.* so be carefull
$latitudeD= latitude;
$longitudeD = longitude;
SELECT event,
SQRT(
POW(69.1 * (coordX - $latitudeD), 2) +
POW(69.1 * ($longitudeD - coordY) * COS(coordX / 57.3), 2)) AS distance
FROM db.table WHERE SQRT(
POW(69.1 * (coordX - $latitudeD), 2) +
POW(69.1 * ($longitudeD - coordY) * COS(coordX / 57.3), 2)) < 50 ORDER BY distance
;
I have a MySQL Routine that is getting records within a 50 mile radius when passed Latitude and Longitude via utilizing the Haversin equation.
While this works great, and is pretty speedy (considering it's searching through 82k records), I am thinking that I can get better performance by creating a similar procedure utilizing a POINT column.
So, in my table I created an extra column called Location, gave it a datatype of POINT, updated my data to pass lat & lon to the Location column. Data is valid, and is fine., and added a Spatial Index
The question is, how can I convert the following query to use the Location column, instead of lat and lon columns.
SET #LAT := '37.953';
SET #LON := '-105.688';
SELECT DISTINCT
BPZ.`store_id`,
3956 * 2 * ASIN(SQRT(POWER(SIN((#LAT - abs(Z.`lat`)) * pi()/180 / 2),2) + COS(#LAT * pi()/180 ) * COS(abs(Z.`lat`) * pi()/180) * POWER(SIN((#LON - Z.`lon`) * pi()/180 / 2), 2))) as distance,
c.`name`,c.`address`,c.`city`,c.`state`,c.`phone`,c.`zip`,c.`premise_type`
FROM
`zip_codes` as Z,
`brand_product_zip` as BPZ
LEFT JOIN `customers` c ON c.`store_id` = BPZ.`store_id`
WHERE
BPZ.`zip` = Z.`zip`
AND
3956 * 2 * ASIN(SQRT(POWER(SIN((#LAT - abs(Z.`lat`)) * pi()/180 / 2),2) + COS(#LAT * pi()/180 ) * COS(abs(Z.`lat`) * pi()/180) * POWER(SIN((#LON - Z.`lon`) * pi()/180 / 2), 2))) <= 50
ORDER BY
distance LIMIT 20
I understand that this has been asked before, however, everything I see points to calculations based on lat and lon and not the POINT column
Updated Code:
SET #lat = 41.92;
SET #lon = -72.65;
SET #kmRange = 80.4672; -- = 50 Miles
SELECT *, (3956 * 2 * ASIN(SQRT(POWER(SIN((#lat - abs(`lat`)) * pi()/180 / 2),2) + COS(#lat * pi()/180 ) * COS(abs(`lat`) * pi()/180) * POWER(SIN((lon - `lon`) * pi()/180 / 2), 2)))) as distance
FROM `zip_codes`
WHERE MBRContains(LineString(Point(#lat + #kmRange / 111.1, #lon + #kmRange / (111.1 / COS(RADIANS(#lat)))), Point(#lat - #kmRange / 111.1, #lon - #kmRange / (111.1 / COS(RADIANS(#lat))))), `Location`)
Order By distance
LIMIT 20
Have you looked into hilbert curves solutions? A spatial index doesn't deliver the exact solution? . With a mysql spatial index you can use mbrcontains:
CREATE TABLE lastcrawl (id INT NOT NULL PRIMARY KEY, pnt POINT NOT NULL) ENGINE=MyISAM;
INSERT
INTO lastcrawl
VALUES (1, POINT(40, -100));
SET #lat = 40;
SET #lon = -100;
SELECT *
FROM lastcrawl
WHERE MBRContains
(
LineString
(
Point
(
#lat + 10 / 111.1,
#lon + 10 / ( 111.1 / COS(RADIANS(#lat)))
),
Point (
#lat - 10 / 111.1,
#lon - 10 / ( 111.1 / COS(RADIANS(#lat)))
)
),
pnt
);
Look here: MySQL - selecting near a spatial point.
Here: http://www.drdobbs.com/database/space-filling-curves-in-geospatial-appli/184410998
The article Nearest-location finder for MySQL explains in detail various options, and the best choice for use with the Spatial Extensions starting with MySQL 5.6.
From the article, this sample query lists zip codes within a 50 mile radius from given coordinates (42.81, -70.81):
SELECT zip, primary_city,
latitude, longitude, distance_in_mi
FROM (
SELECT zip, primary_city, latitude, longitude,r,
69.0 * DEGREES(ACOS(COS(RADIANS(latpoint))
* COS(RADIANS(latitude))
* COS(RADIANS(longpoint) - RADIANS(longitude))
+ SIN(RADIANS(latpoint))
* SIN(RADIANS(latitude)))) AS distance_in_mi
FROM zip
JOIN (
SELECT 42.81 AS latpoint, -70.81 AS longpoint, 50.0 AS r
) AS p
WHERE latitude
BETWEEN latpoint - (r / 69)
AND latpoint + (r / 69)
AND longitude
BETWEEN longpoint - (r / (69 * COS(RADIANS(latpoint))))
AND longpoint + (r / (69 * COS(RADIANS(latpoint))))
) d
WHERE distance_in_mi <= r
ORDER BY distance_in_mi;