我正在使用两个数据库,主数据库和二级数据库。两者都包含相同的架构。现在,如果二级数据库中的数据不是NULL,则无法从辅助数据库中用同一列的内容更新主数据库的列。
ATTACH "secondary.db" as second;
UPDATE main.table set main.value = coalesce((SELECT value FROM second.table),main.value);但是上面的代码不起作用,只有从辅助数据库中的列的第一个值被复制到主数据库中列中的所有值。
就像。主数据库:
index, value
1, 45
2, 56
3, 23二级数据库:
index, value
1, NULL
2, 55
3, NULL预期成果数据库:
index, value
1, 45
2, 55
3, 23有什么建议吗?
在这里,你可以看到我在尝试什么:http://sqlfiddle.com/#!5/845545/1
发布于 2020-08-31 17:32:59
我认为您需要一个相关的子查询:
update main.table t
set main.value = coalesce((select t2.value from second.table t2 where t2.index = t.index),
main.value
);或者更有效率。过滤行,不要使用coalesce()
update main.table t
set main.value = (select t2.value from second.table t2 where t2.index = t.index)
where exists (select 1 from second.table t2 where t2.index = t.index and t2.value is not null)发布于 2020-08-31 16:27:32
我已经有几年没有继续使用SQLite了,但我认为您现在可以使用以下语句中的更新:
UPDATE
main
SET
main.value = Coalesce (main.value, second.value)
FROM
main.table
INNER JOIN
second.table
ON main.id = second.idhttps://stackoverflow.com/questions/63674549
复制相似问题