很抱歉问了这样一个基本问题(我确信它是重复的),但我已经进行了广泛的搜索,但我无法弄清楚如何做我需要的事情。我可能只是在搜索错误的关键字。
我有一个带有空列的数据库表。我想在该列中为所有行插入一个值。
我的表格如下所示:
wkb_geometry | geometry(Polygon,4326) |
id | integer |
area | real |
我想将 wkb_geometry
的面积插入到所有行的面积列中。
我知道如何计算和更新单个行,如下所示:
update inspire_parcel set area=(select st_area(wkb_geometry::geography)
from inspire_parcel where id=24045271) where id=24045271;
但是我怎样才能对所有行执行此操作?
如果有影响的话,我有很多行 - 超过 2000 万行。
最佳答案
不需要选择,只需用函数的结果更新列并省略 where 子句即可更新所有行:
update inspire_parcel
set area = st_area(wkb_geometry::geography);
为了避免覆盖现有值,只需使用适当的 WHERE 子句:
update inspire_parcel
set area = st_area(wkb_geometry::geography)
where area is null;
如果您只想对单行执行此操作,这也是正确的方法:
update inspire_parcel
set area = st_area(wkb_geometry::geography)
where id = 24045271;
关于sql - 基本 Postgres : How do I insert a value into all rows?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27433208/