2

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

Muhammad Sipra
  • 806
  • 2
  • 11
  • 27
  • 1
    Please show the long and lat of your 2 IDs in order to replicate your issue – Thomas G Sep 10 '18 at 14:54
  • @ThomasG i have added actual lat and long – Muhammad Sipra Sep 11 '18 at 05:25
  • Can you tell us how you obtained the 'actual distances'? The query results look correct to me. – wookiekim Sep 11 '18 at 06:05
  • @wookiekim from google API , it just gave "actual lat/lng" of the orders.address and my current lat/lat to measure distance. – Muhammad Sipra Sep 11 '18 at 06:26
  • 1
    If you try to calculate lat long distances of your example coordinates in any other lat long distance calculator, you would see that the query results are actually correct. Maybe the problem is in your usage of google API? – wookiekim Sep 11 '18 at 06:40

0 Answers0