I have a website where users can look up other users. It is a basic search feature, but I want the users’ results to be location based. So maybe they search “David”. The basic query for the would be
SELECT *
FROM users
where fname = 'David' or
lname= 'David'
ORDER BY distance ASC
The problem is that the distance has to be first calculated between each user and the searcher. How can I do this? Not asking to be shown how to calculate distance or anything, just how to logically solve this problem.
EDIT: The thing is to calculate the distance directly in your select statement and to order the result based on this calculation.
So if you stored the latitude/longitude in the database, you should be able to do something like that:
And you might have a much simpler query if you’re using the geo data types in MySQL.