get all markers within a given radius in sql
-
26-09-2019 - |
Question
I'm trying to get all locations stored in the database with an sql statement described here
My sql statement is:
SELECT TOP(10)
*,
(
6371 *
acos(
cos(
radians(<cfqueryparam cfsqltype="CF_SQL_Numeric" value="#FORM.latitude#">)
) *
cos(
radians( custLat )
) *
cos(
radians( custLong ) -
radians(<cfqueryparam cfsqltype="CF_SQL_Numeric" value="#FORM.longtitude#">)
) +
sin(
radians(<cfqueryparam cfsqltype="CF_SQL_Numeric" value="#FORM.latitude#">)
) *
sin(
radians( custLat )
)
)
) AS distance
FROM customers
HAVING distance < 25
ORDER BY distance;
Problem is I get an error and can't figure out why...
error: Invalid column name 'distance'.
on this line: radians(<cfqueryparam cfsqltype="CF_SQL_Numeric" value="#FORM.latitude#">)
(the second occurrence)
Why do I get this error and how do I fix it?
Solution
What you did it wrong. Here is solution :
SELECT distance FROM
(SELECT TOP(10)
*,
(
6371 *
acos(
cos(
radians(<cfqueryparam cfsqltype="CF_SQL_Numeric" value="#FORM.latitude#">)
) *
cos(
radians( custLat )
) *
cos(
radians( custLong ) -
radians(<cfqueryparam cfsqltype="CF_SQL_Numeric value="#FORM.longtitude#">)
) +
sin(
radians(<cfqueryparam cfsqltype="CF_SQL_Numeric" value="#FORM.latitude#">)
) *
sin(
radians( custLat )
)
)
) AS distance
FROM customers
) newTable
HAVING distance < 25
GROUP BY distance
ORDER BY distance;
OTHER TIPS
You can solve it using creating a stored procedure or view in sql server or mysql server.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow