mysql - SQL - Unqiue results based on criteria -


making dealer locator people search dealer near them. want work in such way if dealer has multiple branches near person, closest branch shows. "name" field should unique in results result shown having least distance person searching versus other rows have same "name" field. want closest 5 dealers shown ordered dealer level, or medal. right have following:

$query = sprintf("select name, address, contact, image, medal, phone, email, website, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) distance dealers having distance < 60 order medal, distance limit 0 , 5", mysql_real_escape_string($center_lat), mysql_real_escape_string($center_lng), mysql_real_escape_string($center_lat), mysql_real_escape_string($radius)); $result = mysql_query($query); 

from i've read, sounds need like:

select * ( select row_number() on (partition name order distance) num dealers)a a.num = 1 

or that, can't work right. insights on how can function appreciated.

get minimum distance per dealer , of these take first five. select again table in order complete records, take records identified dealer , distance.

a with clause helpful, mysql doesn't support it. well, ...

select   name, address, contact, image, medal, phone, email, website, lat, lng,   ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) distance dealers (name, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) )) in (   select     name, min( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) distance   dealers   group name   having distance < 60   order distance   limit 5 ) order distance, medal; 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo