获取sql中给定半径内的所有标记

时间:2010-08-05 07:50:32

标签: sql coldfusion google-maps-api-3 distance

我正在尝试使用描述为here

的sql语句来存储数据库中的所有位置

我的sql语句是:

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;

问题是我收到错误,无法弄清楚原因...... 错误:Invalid column name 'distance'.

在这一行:radians(<cfqueryparam cfsqltype="CF_SQL_Numeric" value="#FORM.latitude#">)(第二次出现)

为什么我会收到此错误以及如何解决?

2 个答案:

答案 0 :(得分:2)

你做错了什么。这是解决方案:

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;

答案 1 :(得分:0)

您可以使用在sql server或mysql server中创建存储过程或视图来解决它。