首先,我在两个数据库(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 的新手,但一直在查看文档并四处搜索,但没有任何建议。
最佳答案
这个怎么样:
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
及其带来的困难的常用方法当您有多个结果列时(分组依据
,连接)。
关于sql - 如何将此 mysql 查询转换为 postgres 9.4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31504641/