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

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

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

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并选择最接近的一个。

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

sql postgresql postgis
2个回答
1
投票

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

无论如何,如果我理解你的问题,这应该工作(未经测试)

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
);

1
投票

不确定distance,但IMO这或多或少是你想要的。


UPDATE point_a a
SET c_id = sub.id
FROM (SELECT b.otherid AS id
        , c.lineconnecting_a_id AS line_id
        FROM point_b b 
        JOIN line c ON st_endpoint(c.geom) = b.geom or st_startpoint(c.geom) = b.geom 
        ORDER BY st_distance (c.geom, b.geom)
        limit 1
        )sub
WHERE a.pointid = sub.line_id            
        ;
© www.soinside.com 2019 - 2024. All rights reserved.