MERGE 语法用于对重复的 UPDATE 进行 UPSERT 或 INSERT [英] MERGE syntax used to UPSERT or INSERT on duplicate UPDATE
问题描述
所以我来自 MySQL,我可以在那里做 在重复更新时插入:
So I'm coming from MySQL where I could do INSERT on DUPLICATE UPDATE:
INSERT INTO table (a,b,c)
VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
但现在我正在使用 PostgreSQL 并且正在努力添加 UPSERT 功能,看起来 MERGE 可能适用于我想要的但想看看这是否是最佳语法.示例语法 1、我也看过这个,但不明白如何实施.我还没有尝试过这个,因为我认为 MERGE 用于将数据从表 1 合并到表 2 或者类似这样的工作?
But now I'm using PostgreSQL and there are efforts to add the UPSERT functionality, looks like MERGE might work for what I would like but wanted to see if this is the most optimal syntax. Example Syntax 1, I've also seen this but don't understand how to implement. I haven't tried this yet because I thought MERGE was used for merging data from table1 to Table2 or would something like this work?
MERGE
INTO table
USING table
ON c = 1
WHEN MATCHED THEN
UPDATE
SET c=c+1
WHEN NOT MATCHED THEN
INSERT (a,b,c)
VALUES (1,2,3)
还有其他建议吗?
推荐答案
在 MERGE 可用之前,请使用这种强大的方法:在 PostgreSQL 重复更新时插入?
Until MERGE is available, use this robust approach: Insert, on duplicate update in PostgreSQL?
这篇关于MERGE 语法用于对重复的 UPDATE 进行 UPSERT 或 INSERT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!