0

I'm trying to make a query that select users located on an area. I've this table:

Users (user_id, username, user_latitude, user_longitude)

I have an area defined by 2 points:

  • North West (lat, lng)
  • South East (lat, lng)

How to get the users with a geoposition contained on the area?

Thank you :)

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Pierrick Aubin
  • 55
  • 2
  • 10
  • Can you write the condition in plain English (not SQL) when a user should be deemed belonging to the area? Hint: user's longitude and latitude should each be in some interval. – PM 77-1 Mar 09 '13 at 17:02
  • I guess the query would be something like this: user.lat <= NorthWest.lat && user.lng >= NorthWest.lng && user.lat >= SouthEast.lat && user.lng <= SouthEast.lng, Wouldn't it? – Pierrick Aubin Mar 09 '13 at 17:17
  • Instead of pasting it into the comment add it to your original post. – PM 77-1 Mar 09 '13 at 17:19

1 Answers1

0

A naive but simple approach could look like this, assuming your area is similar to a rectangle:

SELECT * FROM Users WHERE (user_latitude >= 1.2393 AND user_latitude <= 1.5532) 
AND (user_longitude >= -1.8184 AND user_longitude <= -1.654)

For more sophisticated demands, you should use geospatial functions.

cara
  • 1,012
  • 1
  • 9
  • 15