Jump to content

[SOLVED] Linking Tables - Radius Search - query help


advancedfuture

Recommended Posts

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

 

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?

Never Mind! figured out the query all by myself :D yaay!

 

SELECT * FROM businesses, zip_code WHERE businesses.businessZip = zip_code.zip_code AND 
    60 * 1.1515 * DEGREES(ACOS(SIN(RADIANS($cityXlat))
    * SIN(RADIANS(lat)) +  COS(RADIANS($cityXlat))
    * COS(RADIANS(lat)) * COS(RADIANS($cityXlong-lon)))) <= 50

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.