I have the following query in mysql
SELECT id,
(SELECT ( 6371 * Acos(Cos(Radians('31.502537032937294')) *
Cos(Radians(lat)) *
Cos(
Radians(lng) - Radians(
'74.34226725250483'))
+
Sin(
Radians('31.502537032937294')) *
Sin(Radians(lat))) ) AS distance
FROM `addresses`
WHERE orders.id = addresses.order_id
AND `type` = 'from'
AND `address_for` = 'order'
AND `is_archive` = 0
HAVING `distance` <= 50) AS distance
FROM orders
WHERE `is_archive` = 0
AND `is_complete` = 0
AND `status_id` = 1
AND Date(`expected_pick_up_at`) = '2018-09-10'
In which, I am calculating distance of orders from my current position. The result of this query is as following
id distance
1027 3.392817926161619
1028 2.957290773676651
While the actual distance is
id distance
1027 0.09565605656865
1028 0.43565656101560
original lat/long saved in the database from which I am calculating distance are as
id lat long
1027 31.53189017798653 74.35203921049833
1028 31.52842855342999 74.34939790517092
Can anyone guide me which part I am doing wrong in this,
Note: I have also looked at the following question
Related
I have database with a list of contacts with puesdo coordinates of where they live. Here's a sample:
name e_point n_point
David 102 345
James 174 746
Ali 460 584
Kevin 364 479
Mark 385 274
I was wondering is it possible to create a query that can search within a distance of the two coordinates? E.g., I want a list of people who live within a 20 sqr mile radius of James.
What functions can help me do this?
Mysql syntax:
SELECT name FROM `table` WHERE SQRT(
POW(e_point - (SELECT e_point FROM `table` WHERE name='james'), 2) +
POW(n_point - (SELECT n_point FROM `table` WHERE name='james'), 2)) < 20
AND name <> 'james'
Notice:
you need to change 'name' in 3 places.
subqueries added for let you run one query with just one variable (name). If you remove subqueries, you need to run 2 queries (first query retrieving coord, and second query searching near people)
equation is:
Where p1=(p1x, p1y) and p2=(p2x, p2y)
To calculate the distance between 2 coordinates you need to get the Great-circle distance, as the earth is rounded and measurement of distance is affected by this fact.
Doing this with SQL would be something like this according to the Google Maps API docs:
SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) )
AS distance
FROM markers
HAVING distance < 25
ORDER BY distance
LIMIT 0 , 20;
if your coordinates represent like x and y coordinates you can use something like this (just replaces occurrences of contacts with your actual table name) distance returned is the distance from 'James':
SELECT T2.name,
SQRT(POWER(T2.e_point-T1.e_point,2)+
POWER(T2.n_point-T1.n_point,2)) as distance
FROM contacts T1
INNER JOIN contacts T2 ON
SQRT(POWER(T2.e_point-T1.e_point,2)+
POWER(T2.n_point-T1.n_point,2)) <= 20
WHERE T1.name = 'James'
AND T1.name != T2.name
sqlfiddle
I have 2 tables
1. feeds => id,name
2. feed_locations => id, feed_id, latitude,longitude,location_name
A feed have multiple locations.
now i want to search near by feed from user current location
I require user latitude or longitude so i fetch from google location and radius
Now i use this mysql query
SELECT
`Feed`.`id`,
`Feed`.`name`,
(3959 * acos (cos ( radians(40.7127837) ) * cos( radians( FeedLocation.latitude ) ) * cos( radians( FeedLocation.longitude ) - radians(-74.00594130000002) ) + sin ( radians(40.7127837) ) * sin( radians( FeedLocation.latitude )))) AS `distance`,
(Select COUNT(id) FROM feed_locations WHERE feed_id = `Feed`.`id`) AS `location_count` FROM `feeds` AS `Feed`
LEFT JOIN `feed_locations` AS `FeedLocation` ON (`FeedLocation`.`feed_id` = `Feed`.`id`)
GROUP BY `Feed`.`id` HAVING distance < 10
ORDER BY `distance` ASC
This query giving me result but
i require minimum distance of the feed_locations from user current location. Distance field give me highest value but i require lowest value from feed_locations table.
My query is working but issue it that :
Like if a single feed have 10 location lik 1m,2m,3m,4m,5m,10m,100m distance. and i want to find 5m distance all feed then it works but it shows me that this feed have 5m. distance from me but result should be 1m distance.
I have created a Fiddle please check http://sqlfiddle.com/#!9/c408e6/5
According to the fiddle in table 1 result is coming that feed1 is 4.02m from my current location but this feed have 2 location in table2 i have show each of location distance .so feed1 one of location is zero mile distance from me but it give result that feed1 is 4.02mile
SELECT
a.id,
a.name,
a.distance,
a.location_count
FROM
(SELECT
`Feed`.`id`,
`Feed`.`name`,
(3959 * acos (cos ( radians(40.7127837) ) *
cos( radians( FeedLocation.latitude ) ) *
cos( radians( FeedLocation.longitude ) -
radians(-74.00594130000002) ) + sin ( radians(40.7127837) ) *
sin( radians( FeedLocation.latitude )))) AS `distance`,
(Select COUNT(id)
FROM feed_locations
WHERE feed_id = `Feed`.`id`) AS `location_count`
FROM `feeds` AS `Feed`
LEFT JOIN `feed_locations` AS `FeedLocation` ON (`FeedLocation`.`feed_id` = `Feed`.`id`)
ORDER BY `distance` ASC) AS a
GROUP BY a.id HAVING distance < 10;
In the fiddle I worked out this query you see and it works, now result is 0. The point is that if you group a not ordered set of records and then you order... you do not order anything. You have to order and then group the ordered set.
Hope it works for you.
Regards
I'm not quite sure how to expand this query so that I can also have a AVG(price_current) column ... instead of having to calculate this value within PHP once the query comes back.
SELECT
listing_subtype, bedrooms, total_baths, tot_sqft_finished, price_current, latitude, longitude, (
3959 * acos (
cos ( radians(48.639) )
* cos( radians( latitude ) )
* cos( radians( longitude ) - radians(-123.404) )
+ sin ( radians(48.639) )
* sin( radians( latitude ) )
)
) AS distance
FROM rets_property_resi
WHERE listing_subtype = 'Single Family Detached' AND
bedrooms >= 2 AND bedrooms <= 3 AND
total_baths >= 1 AND total_baths <= 2 AND
tot_sqft_finished >= 2000 AND tot_sqft_finished <= 2500
HAVING distance < 5
ORDER BY distance
LIMIT 0, 25;
I'm going to make some assumptions here:
You look for similar houses/homes in a given radius around a requested one. Each of those homes has a primary key in the table you are querying from. Let us call that primKey here.
Additionally, you want the average price of all returned homes which is limited to 0,25, and not the AVG of all records in the table that match you WHERE clause.
All returned rows will have therefore an extra field with the average price and is the same for each row.
You will have to run the same query again as a subquery. However, since you want to limit the AVG you have to run that in a subquery to calculate the correct AVG.
This is really clunky:
SELECT
rets_property_resi.listing_subtype, rets_property_resi.bedrooms, rets_property_resi.total_baths, rets_property_resi.tot_sqft_finished, rets_property_resi.price_current, rets_property_resi.latitude, rets_property_resi.longitude, (
3959 * acos (
cos ( radians(48.639) )
* cos( radians( latitude ) )
* cos( radians( longitude ) - radians(-123.404) )
+ sin ( radians(48.639) )
* sin( radians( latitude ) )
)
) AS distance, outerSubQuery.averagePrice
FROM rets_property_resi
LEFT JOIN
(
SELECT innerSubQuery.primKey AS primKey, AVG(innerSubQuery.price_current) AS averagePrice
FROM
(SELECT
primKey, price_current, (
3959 * acos (
cos ( radians(48.639) )
* cos( radians( latitude ) )
* cos( radians( longitude ) - radians(-123.404) )
+ sin ( radians(48.639) )
* sin( radians( latitude ) )
)
) AS distance
FROM rets_property_resi
WHERE listing_subtype = 'Single Family Detached' AND
bedrooms >= 2 AND bedrooms <= 3 AND
total_baths >= 1 AND total_baths <= 2 AND
tot_sqft_finished >= 2000 AND tot_sqft_finished <= 2500
HAVING distance < 5
ORDER BY distance
LIMIT 0, 25) AS innerSubQuery
GROUP BY innerSubQuery.primKey
) AS outerSubQuery ON (outerSubQuery.primKey = rets_property_resi.primKey)
WHERE listing_subtype = 'Single Family Detached' AND
bedrooms >= 2 AND bedrooms <= 3 AND
total_baths >= 1 AND total_baths <= 2 AND
tot_sqft_finished >= 2000 AND tot_sqft_finished <= 2500
HAVING distance < 5
ORDER BY distance
LIMIT 0, 25;
I bet there is a more elegant version of this though. You might be better off with a temporary table here or keep just calculating in code.
It might be the repeated question but need help.
i am new to MySQL.
Here is my problem.
i have a query to calculate distance between latitude and longitude. based on the distance order i have to return id's.
SELECT dlo.id,
(3959 * acos(cos(radians(12.9)) * cos(radians(y(gproperty))) * cos(radians(x(gproperty)) - radians(77.5)) +sin(radians(12.9)) * sin(radians(y(gproperty))))) AS distance
FROM db1.gfeature dgf,
db2.loc dlo,
db2.cust dcu
WHERE gf.o_type = 6
AND dcu.id = 240
AND dgf.o_id = dlo.p_id HAVING distance < 20
ORDER BY distance LIMIT 10;
which returns as
+------+-----------------------+
| id | distance |
+------+-----------------------+
| 101 | 0.00025714756425665 |
| 199 | 0.10971525612556807 |
| 722 | 0.22772618588406165 |
+------+-----------------------+
But i need only id column to be displayed. I asked same-question yesterday. But now i am using three tables to get data. So confused in joining 3 tables.
Can anybody suggest me.?
I tried this way
select id from (
select
dlo.id,
( 3959 * acos( cos( radians(12.9) )
* cos( radians( y(gproperty) ) )
* cos( radians( x(gproperty) ) - radians(77.5) )
+ sin( radians(12.9) )
* sin( radians(y(gproperty) ) )
)
) AS distance
from db1.gfeature dgf
join db2.cust dcu, db2.loc dlo
on dgf.o_type = 6 and dcu.id = 10 and dgf.o_id = dlo.w_id
) t
where distance < 10
order by distance
limit 10;
but getting syntax error near "on..."
Maybe something like this. Just use subquery to achieve.
SELECT S.ID
FROM
(SELECT dlo.id,
(3959 * acos(cos(radians(12.9)) * cos(radians(y(gproperty))) * cos(radians(x(gproperty)) - radians(77.5)) +sin(radians(12.9)) * sin(radians(y(gproperty))))) AS distance
FROM db1.gfeature dgf, db2.loc dlo , db2.cust dcu
WHERE gf.o_type = 6 AND dcu.id = 240 AND dgf.o_id = dlo.p_id
HAVING distance < 20) S
ORDER BY S.distance
LIMIT 10;
Hi I have the following table
--------------------------------------------
| id | city | Latitude | Longitude |
--------------------------------------------
| 1 | 3 | 34.44444 | 84.3434 |
--------------------------------------------
| 2 | 4 | 42.4666667 | 1.4666667 |
--------------------------------------------
| 3 | 5 | 32.534167 | 66.078056 |
--------------------------------------------
| 4 | 6 | 36.948889 | 66.328611 |
--------------------------------------------
| 5 | 7 | 35.088056 | 69.046389 |
--------------------------------------------
| 6 | 8 | 36.083056 | 69.0525 |
--------------------------------------------
| 7 | 9 | 31.015833 | 61.860278 |
--------------------------------------------
Now I want to get distance between two points. Say a user is having a city 3 and a user is having a city 7. My scenario is one user having a city and latitue and longtitude is searching other users distance from his city. For example user having city 3 is searching. He wants to get distance of user of any other city say it is 7. I have searched and found following query
SELECT `locations`.`city`, ( 3959 * acos ( cos ( radians(31.589167) ) * cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians(64.363333) ) + sin ( radians(31.589167) ) * sin( radians( Latitude ) ) ) ) AS `distance` FROM `locations` HAVING (distance < 50)
As for as I know this query finds distance from one point to all other points. Now I want to get distance from one point to other point.
Any guide line will be much appreciated.
I think your question says you have the city values for the two cities between which you wish to compute the distance.
This query will do the job for you, yielding the distance in km. It uses the spherical cosine law formula.
Notice that you join the table to itself so you can retrieve two coordinate pairs for the computation.
SELECT a.city AS from_city, b.city AS to_city,
111.111 *
DEGREES(ACOS(LEAST(1.0, COS(RADIANS(a.Latitude))
* COS(RADIANS(b.Latitude))
* COS(RADIANS(a.Longitude - b.Longitude))
+ SIN(RADIANS(a.Latitude))
* SIN(RADIANS(b.Latitude))))) AS distance_in_km
FROM city AS a
JOIN city AS b ON a.id <> b.id
WHERE a.city = 3 AND b.city = 7
Notice that the constant 111.1111 is the number of kilometres per degree of latitude, based on the old Napoleonic definition of the metre as one ten-thousandth of the distance from the equator to the pole. That definition is close enough for location-finder work.
If you want statute miles instead of kilometres, use 69.0 instead.
http://sqlfiddle.com/#!9/21e06/412/0
If you're looking for nearby points you may be tempted to use a clause something like this:
HAVING distance_in_km < 10.0 /* slow ! */
ORDER BY distance_in_km DESC
That is (as we say near Boston MA USA) wicked slow.
In that case you need to use a bounding box computation. See this writeup about how to do that. http://www.plumislandmedia.net/mysql/haversine-mysql-nearest-loc/
The formula contains a LEAST() function. Why? Because the ACOS() function throws an error if its argument is even slightly greater than 1. When the two points in question are very close together, the expression with the COS() and SIN() computations can sometimes yield a value slightly greater than 1 due to floating-point epsilon (inaccuracy). The LEAST(1.0, dirty-great-expression) call copes with that problem.
There's a better way, a formula by Thaddeus Vincenty. It uses ATAN2() rather than ACOS() so it's less susceptible to epsilon problems.
Edit 2022 (by Alexio Vay):
As of today the modern solution should be the following short code:
select ST_Distance_Sphere(
point(-87.6770458, 41.9631174),
point(-73.9898293, 40.7628267))
Please check out the answer of Naresh Kumar.
You can use the ST_Distance_Sphere() MySQL built-in function, supported since MySQL 5.7 version and above.
It computes the distance in meters more efficiently.
select ST_Distance_Sphere(point(lng, lat), point(lng,lat))
i.e.
select ST_Distance_Sphere(
point(-87.6770458, 41.9631174),
point(-73.9898293, 40.7628267)
)
Referred from Calculating distance using MySQL
Heres is MySQL query and function which use to get distance between two latitude and longitude and distance will return in KM.
Mysql Query :-
SELECT (6371 * acos(
cos( radians(lat2) )
* cos( radians( lat1 ) )
* cos( radians( lng1 ) - radians(lng2) )
+ sin( radians(lat2) )
* sin( radians( lat1 ) )
) ) as distance
FROM your_table;
Mysql Function :-
DELIMITER $$
CREATE FUNCTION `getDistance`(`lat1` VARCHAR(200), `lng1` VARCHAR(200), `lat2` VARCHAR(200), `lng2` VARCHAR(200)) RETURNS varchar(10) CHARSET utf8
begin
declare distance varchar(10);
set distance = (select (6371 * acos(
cos( radians(lat2) )
* cos( radians( lat1 ) )
* cos( radians( lng1 ) - radians(lng2) )
+ sin( radians(lat2) )
* sin( radians( lat1 ) )
) ) as distance);
if(distance is null)
then
return '';
else
return distance;
end if;
end$$
DELIMITER ;
How to use in your PHP Code
SELECT getDistance($lat1,$lng1,$lat2,$lng2) as distance
FROM your_table.
Here's a MySQL function that will take two latitude/longitude pairs, and give you the distance in degrees between the two points. It uses the Haversine formula to calculate the distance. Since the Earth is not a perfect sphere, there is some error near the poles and the equator.
To convert to miles, multiply by 3961.
To convert to kilometers, multiply by 6373.
To convert to meters, multiply by 6373000.
To convert to feet, multiply by (3961 * 5280) 20914080.
DELIMITER $$
CREATE FUNCTION \`haversine\`(
lat1 FLOAT, lon1 FLOAT,
lat2 FLOAT, lon2 FLOAT
) RETURNS float
NO SQL
DETERMINISTIC
COMMENT 'Returns the distance in degrees on the Earth between two known points of latitude and longitude. To get miles, multiply by 3961, and km by 6373'
BEGIN
RETURN DEGREES(ACOS(
COS(RADIANS(lat1)) *
COS(RADIANS(lat2)) *
COS(RADIANS(lon2) - RADIANS(lon1)) +
SIN(RADIANS(lat1)) * SIN(RADIANS(lat2))
));
END;
DELIMITER;
Not sure how your distance calculation is going on but you need to do a self join your table and perform the calculation accordingly. Something like this probably
select t1.id as userfrom,
t2.id as userto,
( 3959 * acos ( cos ( radians(31.589167) ) * cos( radians( t1.Latitude ) ) *
cos( radians( t1.Longitude ) - radians(64.363333) ) + sin ( radians(31.589167) ) *
sin( radians( t2.Latitude ) ) ) ) AS `distance`
from table1 t1
inner join table1 t2 on t2.city > t1.city
IMPORTANT! Anyone using or copying these calculations MAKE SURE to use least(1.0, (...)) when passing the calculation to the acos() function. The acos() function will NOT take a value above 1 and I have found when comparing lat/lng values that are identical there are times when the calculations come out to something like 1.000002. This will produce a distance of NULL instead of 0 and may not return results you're looking for depending on how your query is structured!
This is CORRECT:
select round(
( 3959 * acos( least(1.0,
cos( radians(28.4597) )
* cos( radians(lat) )
* cos( radians(lng) - radians(77.0282) )
+ sin( radians(28.4597) )
* sin( radians(lat)
) ) )
), 1) as distance
from locations having distance <= 60 order by distance
This is WRONG:
select round(
( 3959 * acos(
cos( radians(28.4597) )
* cos( radians(lat) )
* cos( radians(lng) - radians(77.0282) )
+ sin( radians(28.4597) )
* sin( radians(lat)
) )
), 1) as distance
from locations having distance <= 60 order by distance
The highest rated answer also talks about this, but I wanted to make sure this was very clear since I just found a long standing bug in my query.
Here's a formula I converted from https://www.geodatasource.com/developers/javascript
It's a nice clean function that calculates the distance in KM
DELIMITER $$
CREATE DEFINER=`root`#`localhost` FUNCTION `FN_GET_DISTANCE`(
lat1 DOUBLE, lng1 DOUBLE, lat2 DOUBLE, lng2 DOUBLE
) RETURNS double
BEGIN
DECLARE radlat1 DOUBLE;
DECLARE radlat2 DOUBLE;
DECLARE theta DOUBLE;
DECLARE radtheta DOUBLE;
DECLARE dist DOUBLE;
SET radlat1 = PI() * lat1 / 180;
SET radlat2 = PI() * lat2 / 180;
SET theta = lng1 - lng2;
SET radtheta = PI() * theta / 180;
SET dist = sin(radlat1) * sin(radlat2) + cos(radlat1) * cos(radlat2) * cos(radtheta);
SET dist = acos(dist);
SET dist = dist * 180 / PI();
SET dist = dist * 60 * 1.1515;
SET dist = dist * 1.609344;
RETURN dist;
END$$
DELIMITER ;
You'll also find the same function in different languages on the site;
Maybe someone will come in handy, I managed to implement my task through the FN_GET_DISTANCE function:
SELECT SUM (t.distance) as Distance FROM
(SELECT (CASE WHEN (FN_GET_DISTANCE (Latitude, Longitude, #OLDLatitude, #OLDLongitude)) BETWEEN 0.01 AND 2 THEN
FN_GET_DISTANCE (Latitude, Longitude, #OLDLatitude, #OLDLongitude) ELSE 0 END) AS distance,
IF (#OLDLatitude IS NOT NULL, #OLDLatitude: = Latitude, 0),
IF (#OLDLongitude IS NOT NULL, #OLDLongitude: = Longitude, 0)
FROM `data`, (SELECT #OLDLatitude: = 0) var0, (SELECT #OLDLongitude: = 0) var1
WHERE ID_Dev = 1
AND DateTime BETWEEN '2021-05-23 08:00:00' AND '2021-05-23 20:00:00'
ORDER BY ID DESC) t;