0
votes

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 :)

1
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
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
Instead of pasting it into the comment add it to your original post. - PM 77-1

1 Answers

0
votes

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.