Created
February 23, 2012 16:55
-
-
Save danielcooper/1893730 to your computer and use it in GitHub Desktop.
Kris should have stayed at homeflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This gets X users within a distance of '10' radians | |
user = User.find_me_a_bloody_user | |
result = ActiveRecord::Base.connection.execute(" | |
SELECT | |
((ACOS( SIN( #{user.lat} * PI( ) /180 ) * SIN( latitude * PI( ) /180 ) + COS( #{user.lat} * PI( ) /180 ) * COS( latitude * PI( ) /180 ) * COS( ( | |
#{user.lng} - longitude) * PI( ) /180 ) ) *180 / PI( ) ) *60 * 1.1515) | |
AS `distance` , users | |
FROM `users` | |
HAVING `distance` <=10 | |
ORDER BY `distance` ASC | |
LIMIT 0 , 1" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment