Postgresql 语法错误更新

Postgresql 语法错误更新,postgresql,postgis,spatial-query,Postgresql,Postgis,Spatial Query,使用PostGIS,我有两个表格,第一个包含250个城市的边界,第二个包含世界上所有国家的边界 我试图影响到它所属的每个城市。下面的查询允许我得到我想要的结果 SELECT DISTINCT ON (cities.id) cities.id, country.id FROM cities LEFT JOIN country ON st_intersects(country.geom, cities.geom) 但当我使用此查询时: UPDATE cities SET country_id=su

使用PostGIS,我有两个表格,第一个包含250个城市的边界,第二个包含世界上所有国家的边界

我试图影响到它所属的每个城市。下面的查询允许我得到我想要的结果

SELECT DISTINCT ON (cities.id) cities.id, country.id
FROM cities
LEFT JOIN country ON st_intersects(country.geom, cities.geom)
但当我使用此查询时:

UPDATE cities
SET country_id=subq.id
FROM (SELECT DISTINCT ON (cities.id) country.id
    FROM cities
    LEFT JOIN country ON st_intersects(country.geom, cities.geom)) AS subq
“国家/地区id”列中充满了相同的数字


在使用UPDATE FROM语法时,我遗漏了什么?

您需要将两个查询与连接条件关联起来:

UPDATE cities
  SET country_id=subq.id
FROM (
  SELECT DISTINCT ON (cities.id) country.id
  FROM cities
    LEFT JOIN country ON st_intersects(country.geom, cities.geom)
) AS subq
  where subq.id = cities.id;
但我认为您不需要从sub select开始。您可以将country表直接连接到cities表:

UPDATE cities
  SET country_id = country.id
FROM country 
where st_intersects(country.geom, cities.geom)