首先,我在两个数据库(MySQL和Postgres)中都有相同的表名,列和数据,我用于MySQL 5.6.24的查询可以使用它
select *,abs(longitudes - -81.4563009)+abs(latitudes-28.11657) as LowestNum
from zips group by LowestNum limit 1
当我将相同的查询放入postgres 9.4时,我基本上试图获得最近的位置给出纬度和经度我得到一个错误状态
ERROR: column "zips.zip" must appear in the GROUP BY clause or be used in an aggregate function
SQL state: 42803
Character: 8
然后我改变了一切并做了这个
select *, abs(longitudes - -81.3952524)+abs(latitudes-28.4583868) as
LowestNum from zips group by LowestNum,zips.zip limit 1
我收到此错误
ERROR: column "zips.ziptype" must appear in the GROUP BY clause or be used in an aggregate function
SQL state: 42803
Character: 8
我是postgres的新手但是一直在查看文档并四处搜索,没有任何建议。
答案 0 :(得分:2)
这个怎么样:
select *, abs(longitudes - -81.4563009)+abs(latitudes-28.11657) as LowestNum
from zips
order by LowestNum ascending
limit 1
抱歉,我没有PostgreSQL实例来测试它。
order by [..] ascending/descending limit 1
是一种避免min
和max
的常用方法,以及当您拥有多个结果列(group by
,加入时)所带来的困难。< / p>