scenario: I have a db with three tables, users, locations, stock;
within users I have: id, name, email
within locations I have: id, place, lati, logi
within stock I have: id, user_id, product, total_count, location_id
I am fairly new and I managed to join all the tables:
$qry = "
SELECT COUNT(id)
FROM stock
LEFT
JOIN users
ON stock.user_id= users.id
LEFT
JOIN locations
ON stock.location_id= locations.id ";
But what I am hoping to be able to do is sort it by distance using the lati and logi. So for example, I want everything within 25 miles, sorted closest to furthest.
How can i pull this off giving the scenario?
I did some googling but everything i'm finding show me how I can get the distance between two points, but what I want to be able to use one set of points, and get everything within X miles of it?
Not sure if that all makes sense ?
I have come up with below simplified formula for calculating this in my application :
pow(CentralLati - LatitudeofCircle, 2) +
pow(CentralLongi - LongitudeofCircle, 2) <= 4
where,
CentralLati , CentralLongi => co-ordinates of a point A
LatitudeofCircle, LongitudeofCircle => co-ordinates of the points 25kms around the point A.
You may adjust the above formula to match your database naming conventions.
Hope I understood your requirement correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With