簡體   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