簡體   English   中英

MySQL中計數查詢中的距離計算

[英]Distance calculation in MySQL in the count query

我有一個用戶表,其中包含用戶的緯度和經度信息。 我正在查詢用戶,具體取決於距特定緯度的距離。 為了加快速度,我可以在SELECT語句中使用distance表達式,並使用Having來過濾結果,例如:

SELECT id, 
    (6378.388 * acos(sin(:latitude) * sin(latitude) + 
     cos(:latitude) * cos(latitude) * cos(longitude - :longitude))) as distance
FROM User
Having distance < 60

問題是要對結果進行分頁,我需要在重復查詢中對用戶ID使用Count函數。 如何使用距離表達式過濾結果的確切數量?

您可以使用子查詢來做到這一點:

select count(id) usrcount from 
(SELECT id, 
(6378.388 * acos(sin(:latitude) * sin(latitude) + 
 cos(:latitude) * cos(latitude) * cos(longitude - :longitude))) as distance
 FROM User) subq
Where distance<60

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM