Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql按何处排序别名_Mysql_Sql - Fatal编程技术网

Mysql按何处排序别名

Mysql按何处排序别名,mysql,sql,Mysql,Sql,我想做的是得到最近的经纬度城市。 我的城市表有3个字段:纬度、液化天然气、半径: 我的查询如下所示: SELECT * FROM cities where st_distance_sphere(point(@lat, @lng), point(lat, lng)) <= area LIMIT 1 我的问题是我想按距离订购。圣徒距离_spherepoint@lat,@lng,pointlat,lng表示距离。 但很显然我不能只是假装圣距离_spherepoint@lat,@lng,p

我想做的是得到最近的经纬度城市。 我的城市表有3个字段:纬度、液化天然气、半径: 我的查询如下所示:

SELECT *
FROM cities 
where st_distance_sphere(point(@lat, @lng), point(lat, lng)) <= area LIMIT 1  
我的问题是我想按距离订购。圣徒距离_spherepoint@lat,@lng,pointlat,lng表示距离。 但很显然我不能只是假装圣距离_spherepoint@lat,@lng,pointlat,lng作为距离,并按距离指定顺序


那么,如何根据距离对结果进行排序呢?

我希望查询如下所示:

SELECT c.*,
        st_distance_sphere(point(@lat, @lng), point(lat, lng)) as dist
FROM cities c
HAVING st_distance_sphere(point(@lat, @lng), point(lat, lng)) <= area
ORDER BY dist
LIMIT 1 
这使用了一个MySQL扩展,允许HAVING子句中的条件,即使没有GROUPBY。HAVING子句允许您为条件使用列别名,而不必使用子查询。

尝试使用

select b.* from (
SELECT a.*, st_distance_sphere(point(@lat, @lng), point(a.lat, a.lng)) as distance FROM cities a) b where b.distance<= area order by b.distance limit 1