UPSERT for quot;INSERT INTO tab SELECT * FROM another_tabquot;(UPSERT 用于“INSERT INTO tab SELECT * FROM another_tab)
问题描述
我会怎么做UPSERT"?(INSERT OR UPDATE) 插入到 SQLite 表中时从另一个表中插入多行.
How would I do "UPSERT" (INSERT OR UPDATE) into SQLite table when inserting multiple rows from another table.
我试过了:
INSERT INTO tab_name
SELECT * FROM tmp
ON CONFLICT(id)
DO UPDATE SET
val = excluded.val;
但它给了我:
DO"附近的语法错误
实现这一目标的正确和最有效的方法是什么?
What would be the correct and the most efficient way to achieve that?
推荐答案
您可能遇到了一个名为 的记录陷阱解析歧义 :
You might have hit a documented trap called parsing ambiguity :
当 UPSERT 附加到的 INSERT 语句从 SELECT 语句中获取其值时,可能存在解析歧义.解析器可能无法判断 "ON" 关键字是否引入了 UPSERT 或者它是否是连接的 ON 子句.要解决此问题,SELECT 语句应始终包含 WHERE 子句,即使该 WHERE 子句只是 "WHERE true".)
When the
INSERTstatement to which theUPSERTis attached takes its values from aSELECTstatement, there is a potential parsing ambiguity. The parser might not be able to tell if the"ON"keyword is introducing theUPSERTor if it is theONclause of a join. To work around this, theSELECTstatement should always include aWHEREclause, even if thatWHEREclause is just"WHERE true".)
那么,这样效果更好吗?
So, does this work better?
INSERT INTO tab_name
SELECT * FROM tmp WHERE true
ON CONFLICT(id) DO UPDATE SET val = excluded.val;
这篇关于UPSERT 用于“INSERT INTO tab SELECT * FROM another_tab"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UPSERT 用于“INSERT INTO tab SELECT * FROM another_tab"
基础教程推荐
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 无法解决整理冲突 2021-01-01
