正如标题所说,我的食物有野外food_id(PK)和food_name。 我已经在表格上有这些数据了
food_id|food_name
-----------------
0000001|food1
0000002|food2
0000003|food3
和我的csv
0000001|apple
0000004|banana
0000005|grape
这是我的查询,如果没有重复的PK
copy foodfrom 'd:\testingfood.csv' delimiter '|' csv
但我想将food1更新为apple to apple并插入0000004 | banana和0000005 | grape?
有可能吗?
答案 0 :(得分:1)
您不能在单个COPY
命令中执行此操作。使用临时表和INSERT
ON CONFLICT
,例如:
create temp table tmp_food (like food); -- create temporary table like food
copy tmp_food from 'd:\testingfood.csv' delimiter '|' csv; -- copy to temp table
insert into food (food_id, food_name) -- insert into food from temp table
select food_id, food_name
from tmp_food
on conflict (food_id) do -- update instead of error
update set food_name = excluded.food_name;
drop table tmp_food; -- drop temp table