![](/img/trans.png)
[英]How to sum the values of the column in postgresql on a certain condition?
[英]How to replace values in a column in a table by condition in PostgreSQL?
我在 PostgreSQL 中有一個名為“building”的表。 它有幾列和數百萬行。 每一行都有一個唯一的 id。 看起來像
id tags height building:levels area
1 apartment 58 m 17 109
2 apartment null null 111
3 shed 7 2 75sqm
4 greenhouse 6m 3 159
5 industrial 16 2;4 105
6 commercial 27 8 474
我還有另一個 csv 文件,其中包含列height
和building:levels
的清理數據。 csv 看起來像:
id height building:levels
1 58 17
3 7 2
4 6 3
5 16 4
6 27 8
我想將 csv 文件加入到服務器上的表中,最終結果可能如下所示:
id tags height building:levels area
1 apartment 58 17 109
2 apartment null null 111
3 shed 7 2 75sqm
4 greenhouse 6 3 159
5 industrial 16 4 105
6 commercial 27 8 474
我想替換height
和building:levels
表和 csv 文件中id
相同的級別。 我嘗試從 csv 導入數據,但它沒有替換值,只是添加了新行。 我怎樣才能做到這一點?
可以先在臨時表中導入csv文件數據:
CREATE TEMPORARY TABLE IF NOT EXISTS building_temp LIKE building ;
COPY building_temp (id, height, "building:levels") FROM your_csv_file.csv WITH options_list ;
請參閱構建 options_list 的手冊。
然后從該臨時表更新構建表
UPDATE building AS b
SET height = bt.height
, "building:levels" = bt."building:levels"
FROM building_temp AS bt
WHERE b.id = bt.id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.