我在 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其中id在表和CSV檔案中的相同。我試圖從 csv 匯入資料,但它沒有替換值,只是添加了新行。我怎樣才能做到這一點?
uj5u.com熱心網友回復:
您可以先在臨時表中匯入 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/376570.html
標籤:PostgreSQL的 文件
下一篇:從csv檔案串列創建單個資料幀
