SQL:将查询结果插入到另一张表的三种情况

SQL:将查询结果插入到另一张表的三种情况

一:要插入目标表不存在:

select * into 目标表 from 表 where … 

二:要插入目标表已经存在:

insert into 目标表 select * from 表 where 条件 (where条件可不加)

若两表只是有部分(字段)相同,则

insert into b(col1,col2,col3,col4,…) select col1,col2,col3,col4,… from a where… (where条件可不加)

-- where 条件可去掉
insert into table_a(col1,col2,col3) select col1,col2,col3 from table_b where table_a.uuid = table_b.uuid


三:跨数据库操作:

怎么把A数据库的atable表所查询的东西,全部插入到B 数据库的btable表中

select * into B.btable from A.atable where …

同样,跨服务器操作,也可以。

发布了43 篇原创文章 · 获赞 13 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/qq_41070393/article/details/84143479