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