INSERT INTO 子查询和 ON CONFLICT

INSERT INTO with subquery and ON CONFLICT

我想将 JSON 数组中的所有元素插入 table:

INSERT INTO local_config(parameter, value)
SELECT json_extract(j.value, '$.parameter'), json_extract(j.value, '$.value')
FROM json_each(json('[{"parameter": 1, "value": "value1"}, {"parameter": 2, "value": "value2"}]')) AS j
WHERE value LIKE '%'
ON CONFLICT (parameter) DO UPDATE SET value = excluded.value;

到目前为止这有效,但我真的需要 WHERE value LIKE '%' 子句吗?

当我删除它时:

INSERT INTO local_config(parameter, value)
SELECT json_extract(j.value, '$.parameter'), json_extract(j.value, '$.value')
FROM json_each(json('[{"parameter": 1, "value": "value1"}, {"parameter": 2, "value": "value2"}]')) AS j
ON CONFLICT (parameter) DO UPDATE SET value = excluded.value;

我收到这个错误:

[SQLITE_ERROR] SQL error or missing database (near "DO": syntax error)

来自SQL As Understood By SQLite/Parsing Ambiguity

When the INSERT statement to which the UPSERT is attached takes its values from a SELECT statement, there is a potential parsing ambiguity. The parser might not be able to tell if the "ON" keyword is introducing the UPSERT or if it is the ON clause of a join. To work around this, the SELECT statement should always include a WHERE clause, even if that WHERE clause is just "WHERE true".

因此您需要 WHERE 子句,但它可以是简单的 WHERE true 或只是 WHERE 1.