在mysql查询错误中有子句

时间:2015-12-02 07:51:08

标签: mysql

我正在尝试根据lat和lng获取最近的位置,但我想添加有距离<查询中的子句。

但我可以在哪里使用。??

select p.id, p.name, p_i.image as image, 
( 3959 * acos( cos( radians(26.916279) ) * 
cos( radians( lat ) ) * 
cos( radians( lng ) - radians(75.8082) ) + 
sin( radians(26.916279) ) * 
sin( radians( lat ) ) ) ) AS distance 
from `places` as p 
inner join `place_images` as p_i 
on p.`id` = p_i.`place_id` 
where p.`city_id` = '1' 
and p.status = 'active' 
and p_i.display_order = '1' 
limit 0, 5

当前结果

enter image description here

3 个答案:

答案 0 :(得分:2)

尝试此查询

where p.`city_id` = '1' and p.status = 'active' 
and p_i.display_order = '1' 
HAVING distance < 5
limit 0, 5

最后使用limit

答案 1 :(得分:2)

您最后可以在having

之前添加limit子句
select p.id, p.name, p_i.image as image, ( 3959 * acos( cos( radians(26.916279) )
 * cos( radians( lat ) ) * cos( radians( lng ) - radians(75.8082) )
 + sin( radians(26.916279) ) * sin( radians( lat ) ) ) ) AS distance 
 from `places` as p inner join `place_images` as p_i on p.`id` = p_i.`place_id`
 where p.`city_id` = '1' and p.status = 'active' and p_i.display_order = '1' 
 HAVING distance < 5 limit 0, 5

答案 2 :(得分:1)

select p.id, p.name, p_i.image as image, ( 3959 * acos( cos( radians(26.916279) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(75.8082) ) + sin( radians(26.916279) ) * sin( radians( lat ) ) ) ) AS distance from `places` as p inner join `place_images` as p_i on p.`id` = p_i.`place_id` 
where p.`city_id` = '1' 
and p.status = 'active' 
and p_i.display_order = '1' 
HAVING distance < 5 # add this
limit 0, 5