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

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -