Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I filter a SQL result based on distance when I have the latitude and longitude

    I have a database full of user. This is roughly how the database looks:

    Code:
    | username | sex | birthdate  | zip_code |
    |----------+-----+------------+----------|
    | coolUser |  M  | 02-14-1987 |  90210   |
    | blueUser |  F  | 06-16-1982 |  10011   |
    |     .    |  .  |     .      |    .     |
    |     .    |  .  |     .      |    .     |
    |     .    |  .  |     .      |    .     |
    A user will be able to search the database for another user based on their sex, age and distance. I have another database with a list of zipcodes in the U.S. along with with their latitude and longitude. I have written distance function where it calculates the distance of 2 zipcodes based on their latitude and longitude values. I understand I can sort the users by sex and age by something like this:

    Code:
    SELECT * FROM userList WHERE sex = 'M' AND birthdate > 4-12-1986
    Which brings me to my actual question: Is there any possible way to calculate the distance of the 2 users via an SQL query? Or do I have to sort the user based on age and sex first and then get that list and then filter it by distance? If I go with the second method how do I properly display the first 10 results, and have a pagination feature?

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,092
    Thanks
    23
    Thanked 593 Times in 592 Posts


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •