MsSQL   发布时间:2022-05-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了sql – 批量插入,更新如果在Postgres上冲突(批量提升)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在编写一个数据挖掘程序,批量插入用户数据.

当前的sql只是一个普通的批量插入:

insert into USERS(
    id,username,profile_picture)
select unnest(array['12345']),unnest(array['Peter']),unnest(array['someURL']),on conflict (id) do nothing;

如果冲突,我该如何进行更新?我试过了:

...
    unnest(array['Peter']) as a,unnest(array['someURL']) as b,on conflict (id) do 
update set
    username = a,profile_picture = b;

但是它抛出在表“* SELECT *”中有一个名为“a”的列,但是不能从查询的这一部分引用它.错误.

编辑:

USERS表非常简单:

create table USERS (
    id      text not null primary key,username    text,profile_picture text
);

解决方法

结果是一个名为excluded的特殊表包含要插入的行
(奇怪的名字)
insert into USERS(
    id,unnest(array['someURL'])
on conflict (id) do 
update set
    username = excluded.username,profile_picture = excluded.profile_picture;

http://www.postgresql.org/docs/9.5/static/sql-insert.html#SQL-ON-CONFLICT

大佬总结

以上是大佬教程为你收集整理的sql – 批量插入,更新如果在Postgres上冲突(批量提升)全部内容,希望文章能够帮你解决sql – 批量插入,更新如果在Postgres上冲突(批量提升)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: