tags:

views:

26

answers:

0

How to Join Tables with Distance Calculation

I am trying to join two tables together. First is a calendar of events with lat/lng sorted by distance. I also need to get the business information from the directory.

This one works without joining the tables:

$query = "SELECT calendar.id,calendar.coupon,( 3959 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) AS distance FROM calendar HAVING distance < $miles";

This is the code that does not work, have tried many variations:

$query = "SELECT calendar.id,calendar.coupon,directory.business,( 3959 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) AS distance FROM calendar RIGHT JOIN directory ON calendar.id = directory.id HAVING distance < $miles";

If anyone can help it would be much appreciated