我有以下表格和关系:
Players:
foreign_key to User
Users:
foreign_key to City
Cities:
latitude (float)
longitude (float)
我正在尝试获取关联用户来自给定城市或 near_by 城市的所有教授。
我有这个查询,它返回给定特定纬度和经度的 near_by(20 公里)城市以及到给定城市纬度和经度的距离。在这种情况下latitude = 41.353312 and longitude = 1.976252
:
SELECT cities.*,
6371 *
acos(cos(radians(41.353312)) *
cos(radians(cities.latitude)) *
cos(radians(cities.longitude) - radians(1.976252)) +
sin(radians(41.353312))*sin(radians(cities.latitude)))
AS km_away FROM `cities` GROUP BY km_away ASC HAVING km_away <= 20
我已经尝试了这个查询(它不起作用),我希望它返回给我的关联用户城市在给定城市附近的玩家(纬度和经度)。
SELECT COUNT(*)
FROM `players`
INNER JOIN `users`
ON `users`.`id` = `players`.`user_id`
INNER JOIN `cities`
ON `cities`.`id` = `users`.`city_id`
WHERE
(
cities.id IN
(SELECT cities.*,
6371 *
acos(cos(radians(41.353312)) *
cos(radians(cities.latitude)) *
cos(radians(cities.longitude) - radians(1.976252)) +
sin(radians(41.353312))*sin(radians(cities.latitude))) AS km_away
FROM `cities`
GROUP BY km_away ASC
HAVING km_away <= 20)
)
我也想要:
1 - 获取按其关联用户城市到给定城市的距离排序的玩家。
2 - 获取返回的每条记录中的距离(值)。