Supabase POSTGIS SQL 函数返回距用户给定距离内的记录

问题描述 投票:0回答:1

作为前端工程师,我不熟悉 SQL 或 PostGIS,但我必须使用 Supabase 构建这个后端 API。我遵循了他们的文档,这几乎是我所需要的,除了这将返回所有记录。

我想传递第三个参数

distance
:

create or replace function nearby_flies(lat float, long float)
returns table (pattern public.submitted_images.pattern%TYPE, dist_meters float)
language sql
as $$
  select pattern, st_distance(location, st_point(long, lat)::geography) as dist_meters
  from public.submitted_images
  order by location <-> st_point(long, lat)::geography;
$$;

我的表写为:

create table
  public.submitted_images (
    user_id uuid not null,
    created_at timestamp with time zone not null default now(),
    bucket_id text null,
    pattern text null,
    location geography (point) not null,
    constraint submitted_images_pkey primary key (created_at),
    constraint submitted_images_pattern_fkey foreign key (pattern) references pattern (label),
    constraint submitted_images_user_id_fkey foreign key (user_id) references auth.users (id)
  ) tablespace pg_default;

create index submitted_images_geo_index
  on public.submitted_images
  using GIST (location);

并且我已确认该表正在记录

POINT
记录。

如何重写此查询以仅考虑

X
距离内的记录作为附加参数?

sql postgis supabase-database supabase-function
1个回答
0
投票
create or replace function nearby_flies(lat float, long float, dist float)
returns table (pattern public.submitted_images.pattern%TYPE, dist_meters float)
language sql
as $$
  select pattern, st_distance(location, st_point(long, lat)::geography) as dist_meters
  from public.submitted_images
  where dist_meters < dist
  order by location <-> st_point(long, lat)::geography;
$$;
© www.soinside.com 2019 - 2024. All rights reserved.