Problem: Find the closest bar that serves the drink(s) I'm looking for.
Here you can find the generated MySQL code http://pastebin.com/5Uc2ewUW
The API Request that interacts with this problem will have these parameters
query, String, ideally the drink name
lng, double, the starting longitude
lat, double, the starting latitude
range, integer, max distance in meters (with a default value)
The query parameter may select more than one drink (think about searching for "Vodka").
What would be a good strategy to write a SQL query with good performance?
I'm not very expert, but my idea is to
- SELECT bars in the range
- SELECT from drink__bars where bar_id is in the previous select result
- JOIN the drinks table to get drinks data
How do I set the order based on the distance?
Any suggestion is appreciated!
Edit:Thanks for the answers till now, but they are mainly focused on calculate distance and that is covered. I don't get how to order result (that are drinks) based on the distance I have from bars.
Think like this meta query
SELECT drink.id, drink.name
FROM $DATA_POOL
WHERE drink.name LIKE '%MY_QUERY%'
ORDER BY $ORDER
where
$DATA_POOL
= a subset of drinks, that are server in the bars near me (I can already compute which bars are near me)$ORDER
= the distance I have from the bar, based on the API paramslng
andlat