gpt4 book ai didi

mysql - MySQL中计数查询中的距离计算

转载 作者:行者123 更新时间:2023-11-29 20:50:54 27 4
gpt4 key购买 nike

我有一个用户表,其中包含用户的纬度和经度信息。我根据距特定纬度的距离查询用户。为了加快速度,我可以在 SELECT 语句中使用距离表达式,并使用 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

关于mysql - MySQL中计数查询中的距离计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38071484/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com