select * from `users`
where exists (
select *, (6371 * acos(cos(radians(22.559648)) * cos(radians(`lat`)) * cos(radians(`lng`) - radians(88.415552)) + sin(radians(22.559648)) * sin(radians(`lat`)))) AS distances
from `locations`
where `users`.`location_id` = `locations`.`id`
and (6371 * acos(cos(radians(22.559648)) * cos(radians(`lat`)) * cos(radians(`lng`) - radians(88.415552)) + sin(radians(22.559648)) * sin(radians(`lat`)))) < 32.688888
ORDER BY `distances` DESC)
答案 0 :(得分:2)
您需要在主查询中输入ORDER BY
。子查询仅用于判断行是否存在,顺序无关紧要。
但是distances
不是主查询中的一列,因此您不能通过编写的查询对其进行排序。您无需使用EXISTS
,而需要与子查询联接。
SELECT u.*, (6371 * acos(cos(radians(22.559648)) * cos(radians(`lat`)) * cos(radians(`lng`) - radians(88.415552)) + sin(radians(22.559648)) * sin(radians(`lat`)))) AS distances
FROM users AS u
JOIN locations AS l ON `u`.`location_id` = `l`.`id`
HAVING distances < 32.688888
ORDER BY distances DESC