Count query returning unexpected results-Collection of common programming errors
I have this query:
SELECT
count(*) as count ,
( 3959 * acos(
cos( radians( 37.774929 ) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians( -122.419418 ) )
+ sin( radians( 37.774929 ) ) * sin( radians( lat ) )
) ) AS distance
FROM users
HAVING distance < 150
I thought it was going to give me the count of users who are in the radius of 150 miles. But instead it gave me a total number of users. And if lat/lng were different, it would give me zero number of users when there were some users there.
Any ideas how to change this query in order to get the number of users within the 150mi radius?
Thanks!