大家好,我正在postgresql数据库中通过纬度和经度查找最近的位置.但当我运行下面的查询时,它显示的列距离并不存在.

ERROR:  column "distance" does not exist
LINE 1: ... ) ) ) AS distance FROM station_location   HAVING distance <...
                                                             ^
********** Error **********

ERROR: column "distance" does not exist
SQL state: 42703
Character: 218

CREATE TABLE station_location
(
  id bigint NOT NULL DEFAULT nextval('location_id_seq'::regclass),
  state_name character varying NOT NULL,
  country_name character varying NOT NULL,
  locality character varying NOT NULL,
  created_date timestamp without time zone NOT NULL,
  is_delete boolean NOT NULL DEFAULT false,
  lat double precision,
  lng double precision,
  CONSTRAINT location_pkey PRIMARY KEY (id)
)

SELECT  *,( 3959 * acos( cos( radians(6.414478) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(12.466646) ) + sin( radians(6.414478) ) * sin( radians( lat ) ) ) ) AS distance 
FROM station_location
HAVING distance < 5
ORDER BY distance
LIMIT 20;

推荐答案

select * from (
SELECT  *,( 3959 * acos( cos( radians(6.414478) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(12.466646) ) + sin( radians(6.414478) ) * sin( radians( lat ) ) ) ) AS distance 
FROM station_location
) al
where distance < 5
ORDER BY distance
LIMIT 20;

Postgresql相关问答推荐

使用pg_repack从PostgreSQL中的表中删除OID

Psql:Windows 10上奇怪的错误输出编码

Postgres查询按月和年分组的总计数

Rust中使用sqlx的多线程postgres操作的惯用方法是什么?

如何在docker-compose中使用docker secrets设置ConnectionString?

postgres 如何计算多列哈希?

TimescaleDB 连续聚合:如何存储连续聚合结果

将 postgres 从属提升为主 node

PostgreSQL比较两个jsonb对象

如何让 Flask SQLAlchemy 重用数据库连接?

PostgreSQL:根据排序顺序仅 Select 每个 id 的第一条记录

brew 程序更新恼人的错误信息

如何 Select 列值为空的行?

没有查询要创建的in ... error

Postgres - 如果找不到记录,则在更新时返回错误

获取全文搜索配置语言的完整列表

为什么 rake db:migrate 有时会在 structure.sql 中添加尾随空格?

Rails PG::UndefinedTable:错误:missing FROM-clause entry for table

没有函数匹配给定的名称和参数类型

在子类的 Hibernate 中 for each 表指定不同的序列