如何编写SQL查询以使用txt或csv文件从第一行开始更新表列的行?
例如,在更新之前,表可能看起来像
column_1 column_2
1 null
2 null
3 null
然后在更新后,所需的结果将是
column_1 column_2
1 this
2 that
3 those
我尝试插入数据,但这仅将其追加到行的底部,而不是替换现有的空值。
使用相同的示例输入,此输出看起来像
column_1 column_2
1 null
2 null
3 null
null this
null that
null those
答案 0 :(得分:0)
将数据加载到表中。
然后使用update
运行join
:
update original o
set column_2 = t.column_2
from csv_table t
where o.column_1 = t.column_1;
不过,对于您的示例,截断原始表并将新数据复制到其中可能更有意义。
答案 1 :(得分:0)
首先,为文本文件创建一个file_fdw
外部表。
然后使用INSERT ... ON CONFLICT
更新表。这将更新表中已经存在的行并添加新行。