0
SELECT city, (6372.797 * acos(cos(radians({$latitude})) * cos(radians(`latitude_range`)) * cos(radians(`longitude_range`) - radians({$longitude})) + sin(radians({$latitude})) * sin(radians(`latitude_range`)))) AS distance FROM cities WHERE active = 1 HAVING distance > 25 ORDER BY distance ASC

我喜欢能够抓取HAVING距离大于 25KM 小于 50KM 的所有城市。我尝试输入的任何内容都会导致大于 25KM 的所有城市或错误。

如何添加HAVING distance > 25 AND distance <= 50到我的 SQL 查询中?

4

1 回答 1

8

正是您在问题中的方式:

SELECT city, (6372.797 * acos(cos(radians({$latitude})) * cos(radians(`latitude_range`)) * cos(radians(`longitude_range`) - radians({$longitude})) + sin(radians({$latitude})) * sin(radians(`latitude_range`)))) AS distance
FROM cities
WHERE active = 1
HAVING distance > 25 and distance <= 50
ORDER BY distance ASC;

就像一个小提示:使用该having子句过滤列别名(如distance)是 MySQL 扩展。在大多数数据库中,您必须使用子查询。

于 2013-09-18T12:08:48.480 回答