【发布时间】:2020-08-04 12:21:32
【问题描述】:
我需要根据与任何给定点的距离从数据库中取出值,然后按距离升序排序。经度和纬度作为浮点字段保存在数据库中。这是我的查询
SELECT
*,
@distance : = 6371 * 2 * asin(sqrt(POW(sin(({lat} - radians(address.latitude)) / 2), 2) + cos({lat}) * cos(radians(address.latitude)) * POW(sin(({lon} - radians(address.longitude)) / 2), 2)))
FROM
service,
provider,
address
WHERE
service.provider_id = provider.id
AND provider.address_id = address.id
AND provider.status = True
AND
(
6371 * 2 * asin(sqrt(POW(sin(({lat} - radians(address.latitude)) / 2), 2) + cos({lat}) * cos(radians(address.latitude)) * POW(sin(({lon} - radians(address.longitude)) / 2), 2)))
)
< 10
order by
@distance
我需要为 where @distance 等条件重用 distance 但我无法重用,它返回空列表。而 ORDER BY 中的 @distance 工作正常。如何在 where 子句中重用变量?
【问题讨论】:
-
'如何在 where 子句中重用变量?'您不能按操作顺序在 select 之前执行 where ,因此该变量不存在。请研究sql操作顺序。
标签: mysql database django-models