SQL查询中的未知列纬度

时间:2019-04-07 10:23:00

标签: sql

我正在查询下面运行,它给出了错误。我尝试了许多解决方案,但没有输出。请帮帮我。在select语句中工作得很好,但在Haven子句中却不能。

SELECT `users`.`id`, ROUND((3959 * acos(cos(radians(1)) * cos(radians(location.lat)) * cos(radians(location.lng) - radians(1)) + sin(radians(1)) * sin(radians(location.lat)))), 2) as distance
FROM `users`
LEFT JOIN `location` ON `users`.`id` = `location`.`user_id`
WHERE `users`.`id` <> 7

SELECT `users`.`id`
FROM `users`
LEFT JOIN `location` ON `users`.`id` = `location`.`user_id`
WHERE `users`.`id` <> 7 
HAVING ROUND((3959 * acos(cos(radians(1)) * cos(radians(location.lat)) * cos(radians(location.lng) - radians(1)) + sin(radians(1)) * sin(radians(location.lat)))), 2) <= 1000

2 个答案:

答案 0 :(得分:0)

根据此答案:Unknown column in 'having clause'。您必须在select子句中指定location.latlocation.lng

SELECT `users`.`id`, location.lat, location.lng 
FROM `users`
LEFT JOIN `location` ON `users`.`id` = `location`.`user_id`
WHERE `users`.`id` <> 7 
HAVING ROUND((3959 * acos(cos(radians(1)) * cos(radians(location.lat)) * cos(radians(location.lng) - radians(1)) + sin(radians(1)) * sin(radians(location.lat)))), 2) <= 1000

答案 1 :(得分:0)

因为您需要将ROUND((3959 * acos(cos(radians(1)) *..., 2)包装在诸如

的聚合中

having avg( ROUND((3959 * acos(cos(radians(1)) *..., 2) ) <= 1000

having sum( ROUND((3959 * acos(cos(radians(1)) *..., 2) ) <= 1000等。

,您需要在group by users.id子句之前添加having

非聚合列或计算列不能包含在hading子句中。