sql - 如何在postgresql中的where条件的较低值之间进行选择?

标签 sql postgresql postgis

这个很难解释,但基本上我想选择距离较小的条件。查询如下:

update point_a a
set c_id = (select b.otherid
from point_b b, line c
where a.pointid = c.lineconnecting_a_id 
and (st_endpoint(c.geom) = b.geom or st_startpoint(c.geom) = b.geom order by distance limit 1

基本上在最后一行我想用这条线来选择它连接的点,我想要更接近原点的点。问题是,对于我的 OR,我得到 2 分,但我不知道如何限制才能使用 st_distance 并选择最接近的。

换句话说,对于每一行,我需要根据它们到原点的距离来选择起点或终点

最佳答案

请发布有效查询。和表定义。这也可能使您更容易理解您的问题

无论如何,如果我正确理解你的问题,这应该有效(未经测试)

update point_a a
set c_id = (

  select  otherid
  from (
    select b.otherid, distance
    from point_b b, line c
    where a.pointid = c.lineconnecting_a_id 
    and (st_endpoint(c.geom) = b.geom)
   UNION
    select b.otherid, distance
    from point_b b, line c
    where a.pointid = c.lineconnecting_a_id 
    and (st_startpoint(c.geom) = b.geom)
  )
  order by distance limit 1
);

关于sql - 如何在postgresql中的where条件的较低值之间进行选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47355554/

相关文章:

sql - 超过1个sql查询结果

mysql - 如果表中的所有列都是某个唯一键的一部分,可以吗?

postgresql - 带基数的 Postgres percentile_cont

postgresql - 将 osm 文件导入 postgres/postgis 数据库

sql - 在 PostGIS 中查找包含一个点的所有多边形

ruby-on-rails - 关系 "venues"的新行违反了检查约束 "enforce_srid_latlon"

mysql - 在mysql中选择更多范围

postgresql - Postgres : Permission denied for schema even though grants were given

postgresql - Sequelize : How to do a COUNT for a 3rd level included model

SQL - 如果一个表中的列中的字符串包含连接表中的列中的字符串