Mysql命令由别名排序

时间:2016-10-04 11:31:33

标签: mysql sql

我想做的是获得最近的纬度和经度城市 我的城市表有3个字段:lat,lng,radius:
我的查询如下:

Line = "0,.200 p,1.0709 q,1.167 r,1.177 s,1.216 t,1.570 u,1.5843 v,1.6883 w,1.9079 e,.2645"
LineItems = Split(Line, ",")

Dim LineSubItems()      ' has to be Variant or Variant() array
ReDim LineSubItems(0 To UBound(LineItems))

For i = 0 To UBound(LineItems)
    LineSubItems(i) = Split(LineItems(i), " ")
Next

Debug.Print LineSubItems(1)(1) ' "p"

我的问题是我想按距离订购。 (SELECT * FROM cities where st_distance_sphere(point(@lat, @lng), point(lat, lng)) <= area LIMIT 1 )给出距离 但显然我不能只是别名st_distance_sphere(point(@lat, @lng), point(lat, lng))并指定st_distance_sphere(point(@lat, @lng), point(lat, lng)) as distance

那么我如何根据距离订购结果?

2 个答案:

答案 0 :(得分:0)

我希望查询看起来像这样:

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,也允许GROUP BY子句中的条件。 HAVING子句允许您为条件使用列别名而无需使用子查询。

答案 1 :(得分:0)

尝试使用

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