Complex MySQL Query - Radius Search - Linking Tables

Discussion in 'MySQL' started by advancedfuture, Jun 24, 2008.

  1. #1
    OK so I have been developing this website in which I list businesses for sale. And I've been working on a radius search feature. my database looks as follows. I have 2 tables.

    First table 'businesses'
    Fields:

    businessName
    businessAddress
    businessCity
    businessZip


    Second Table I have a list of all us Cities / Zips / Lat Long
    Fields:

    id
    zip_code
    city
    county
    state_name
    state_prefix
    lat
    long


    now I've managed to use the code made by Barand do do distance calculations

    
    SELECT * FROM cities
    WHERE
        60 * 1.1515 * DEGREES(ACOS(SIN(RADIANS($cityXlat))
        * SIN(RADIANS(latitude)) +  COS(RADIANS($cityXlat))
        * COS(RADIANS(latitude)) * COS(RADIANS($cityXlong-longitude)))) <= 50
    
    Code (markup):
    However, I only want to return results in the table 'businesses'. I'm assuming I have to link the tables, I was figuring by zip. But how would I construct the query to not only link the tables but only return results that exist in the table businesses within the specified range?
     
    advancedfuture, Jun 24, 2008 IP
  2. bokiatenxi

    bokiatenxi Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i think a normal inner left join will do...

    SELECT t1.*,t2.* FROM businesses AS t1 LEFT JOIN cities AS t2 ON t1.businessZip = t2.zip_code
    WHERE
    60 * 1.1515 * DEGREES(ACOS(SIN(RADIANS($cityXlat))
    * SIN(RADIANS(t2.latitude)) + COS(RADIANS($cityXlat))
    * COS(RADIANS(t2.latitude)) * COS(RADIANS($cityXlong-t2.longitude)))) <= 50
     
    bokiatenxi, Jun 25, 2008 IP
  3. SLoB

    SLoB Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    an inner join would be better, you already have ALL cities and you only want what is in business
    Left joins are bad (well not good ;) )
     
    SLoB, Jun 27, 2008 IP