php - Codeinginter : joining 2 tables based on 2 parameters -
i have 2 database tables quotations
, garag
. both tables have lat, lng field .
i have show quotation nearby garages based on defined radius.
for have take lat,lng quotations table , match lat,lng of garages table
i can nearby garages using following query in ci
$sql = "select *, ( 3959 * acos( cos( radians(" . $lat . ") ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(" . $lng . ") ) + sin( radians(" . $lat . ") ) * sin( radians( lat ) ) ) ) distance garage having distance < 25"; $result = $this->db->query($sql) ;
but not sure how related quotations requested lat , lng
can please me
select q.* garage g left join quotes q on sqrt(square(abs(q.lat - g.lat)) + square(abs(q.lng - g.lng))) <= @maxdistanceindegrees g.id = @garageid
this should work. note if have large database, problem becomes more complicated, , you'll need create sort of grid system mitigate n^2 issue.
Comments
Post a Comment