我有两个具有不同列的表,如下所示:
table1
(
_id,
title,
name,
number,
address
)
table2
(
_id,
phone,
name,
address
)
如何将数据“名称”、“地址”从表 1 复制到表 2。
我的问题有两种情况:
- 第一:table1、table2在同一个数据库文件中
- 第二:data1.db文件中的table1,data2.db文件中的table2
SQL 中的复制工作如下:
insert into table2 (name, address)
select name, address
from table1
如果该列的值id_
是一样的,需要插入和更新
insert into table2 (name, address)
select name, address
from table1 t1
where not exists (select * from table2 t2 where t1._id = t2._id)
;
update table2 t2 name = (select name from table1 t2 where t1._id = t2._id)
;
update table2 t2 address = (select address from table1 t2 where t1._id = t2._id)
如果您需要在数据库之间复制列,您首先将它们导出到一个文件中(使用您喜欢的任何格式,例如 CSV),然后将该文件手动合并到第二个数据库中,因为您无法编写一条 SQL 来表示“使用这些 sqlite 结构”。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)