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?
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