Knowledge Base Article

[Customer Contribution] Search For User By Radius SQL

This post is from one of our customers!

Looking forward to that free shirt! In the mean time, here's a fun sql snippet I wrote to find select users (for something I was working on) by radius of area:
SELECT
    location.id,location.name,location.address,count(distinct user.id) as user,
    (
        6371 * acos(
            cos( radians( your.latitude )) *
            cos( radians( location.lat )) *
            cos( radians( location.lat ) - radians( your.longtitude )) +
            sin( radians( your.latitude )) *
            sin( radians( location.long ))
        )
    ) `distance`
FROM
    location LEFT JOIN user ON location.id=user.location_id
GROUP BY
location.id
HAVING
    `distance` < 100
ORDER BY
    `distance`
LIMIT
    25
Updated 03-02-2023
No CommentsBe the first to comment