1

I have a query that searches for the all rows in some distance according to geo coordinates:

SELECT LOCATIONS.*, 
       ( ( ACOS(
                  SIN(" . $location->Lat . " * PI() / 180)
                  * SIN(LAT * PI() / 180) 
                  +
                  COS(" . $location->Lat . " * PI() / 180)
                  * COS(LAT * PI() / 180) 
                  * COS( ( " . $location->Lng . " - LNG ) * PI() / 180)
               )
               * 180 / PI()
         )
         * 60 * 1.1515
       ) AS DISTANCE 
FROM   LOCATIONS 
HAVING DISTANCE <= 100 
ORDER  BY DISTANCE ASC 

I'm wondering if it's possible to search for the first record of the table and then treat it as a base - use its lat and lng coordinates?

I was trying achieving this with subquery - but with no luck.

Can anybody give me a suggestion?

falsarella
  • 12,217
  • 9
  • 69
  • 115
DaveW
  • 297
  • 1
  • 5
  • 17

1 Answers1

1

Put the "base" record by itself into a temporary table using CREATE TEMPORARY TABLE AS SELECT ... LIMIT 1. Then join against the temporary table from the query above.

See http://dev.mysql.com/doc/refman/5.0/en/create-table-select.html

schtever
  • 3,210
  • 16
  • 25
  • Thank you for the answer. I didn't even thought about temporary table - that makes sense. – DaveW Feb 01 '12 at 16:26