有没有任何方法可以简化这个更新查询在Sqlite?
update people set
phone = (select phone from people where id=2),
email = (select email from people where id=2)
where id=1;显然,查询将一些字段从一个人复制到另一个人。
如果这是针对几个字段,这种方式在我看来是非常没有效率的,因为它执行了很多子查询。
有优化吗?
发布于 2014-08-06 10:42:37
更新语法不允许一次查找多个值。
也许可以使用替换:
INSERT OR REPLACE INTO people(id, phone, email, other, fields)
SELECT old.id, new.phone, new.email, old.other, old.fields
FROM people AS old,
people AS new
WHERE old.id = 1
AND new.id = 2..。但这实际上会在重新插入之前删除记录,这将更加糟糕。
最简单的方法是使用两个命令:
SELECT phone, email FROM people WHERE id = 2;
UPDATE people SET phone = ?, email = ? WHERE ID = 1;https://stackoverflow.com/questions/25158128
复制相似问题