PostgreSQL Where子句使用计算的距离

时间:2017-11-09 16:39:21

标签: postgresql

我有一个PostgreSQL查询,如下所示:

SELECT *, 
2 * 3961 * asin(sqrt((sin(radians((latitude - 40.2817993164062) / 2))) ^ 2 + cos(radians(40.2817993164062)) * cos(radians(latitude)) * (sin(radians((longitude - -111.720901489258) / 2))) ^ 2)) as distance, 
(SELECT json_agg(deals.*) FROM deals WHERE vendors.id = deals.vendorid) as deals FROM vendors  
WHERE ( category = 'Food' ) 
AND (distance < 80)  
AND (nationwide IS FALSE OR nationwide is NULL) 
ORDER BY featured ASC, created DESC, distance ASC

我使用第二个选择部分以英里为单位。

问题是说AND (distance < 80)的部分我收到以下错误:column "distance" does not exist奇怪的是,如果我移除AND (distance < 80)它的工作方式,它也可以按距离正确排序,输出的数据也包括距离,所以它正确地抓住距离,但由于某种原因,我不会让距离作为WHERE条款中的过滤器,我无法弄清楚原因。

1 个答案:

答案 0 :(得分:1)

距离只是一个别名。你可以尝试类似的东西:

num
相关问题