从另一个表中插入多行时,如何将“UPSERT”(插入或更新)插入到 SQLite 表中。
我试过:
INSERT INTO tab_name
SELECT * FROM tmp
ON CONFLICT(id)
DO UPDATE SET
val = excluded.val;
但它给了我:
syntax error near "DO"
实现该目标的正确且最有效的方法是什么?
最佳答案
您可能遇到了一个记录在案的名为 parsing ambiguity 的陷阱:
When the
INSERT
statement to which theUPSERT
is attached takes its values from aSELECT
statement, there is a potential parsing ambiguity. The parser might not be able to tell if the"ON"
keyword is introducing theUPSERT
or if it is theON
clause of a join. To work around this, theSELECT
statement should always include aWHERE
clause, even if thatWHERE
clause is just"WHERE true"
.)
那么,这样效果更好吗?
INSERT INTO tab_name
SELECT * FROM tmp WHERE true
ON CONFLICT(id) DO UPDATE SET val = excluded.val;
关于sql - "INSERT INTO tab SELECT * FROM another_tab"的 UPSERT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60981306/