繁体   English   中英

如何在PostgreSQL中使用新值更新列

[英]How can I update a column with new values in postgresql

我有一个名为nyc的表,该表包含3列,id,geom和area.Geom列的类型为geometry。 我想计算表中每个几何的面积,并将其计算到nyc表的area列。

我尝试使用以下命令,但无法正常工作:

update nyc as p set area=(select st_area(geom) from nyc) where p.id=nyc.id;

它给我一个错误,说:

LINE 1: update nyc as p set area=(select st_area(geom) from nyc...
                                       ^
HINT:  You will need to rewrite or cast the expression.

由于id是唯一列,因此我假设我应该根据id更新列。 我有点困惑如何连续更新值。感谢您的帮助。

谢谢

您可以将UPDATE语句重写为如下所示

update nyc as p 
set area = tab.new_area
from (
select id, st_area(geom) as new_area from nyc
) as tab 
where p.id = tab.id;

如果id是主键,则不需要任何子选择:

update nyc
   set area = st_area(geom);

这将更新表中的所有行,将area列的现有值替换为新的行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM