将2个表的内容合并为一个表的多种方法? 要有具体事例

angel8601 2006-07-31 09:04:33
因为是新手所以这个比较简单的问题还没搞懂,有知道的大哥帮忙下,给个例子最好,
比如有表A,B.
要求将2个表中的所有数据或部分数据合并后放到另一个表中
要有具体的表和行列数据(有可能2个列的行数不相当)
详细的最好,我好分析一下,要不看不懂

大家帮帮忙啊.
谢谢咯...
...全文
173 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
LouisXIV 2006-07-31
  • 打赏
  • 举报
回复
--prepare test data
declare @testa table
(
id int,
a int
)
insert into @testa
select 1,2 union all
select 2,3

declare @testb table
(
id int,
b int
)
insert into @testb
select 2,3 union all
select 3,4

--1 by union/union all
--1.1 union
select * from @testa
union
select * from @testb
--1.2 union all
select * from @testa
union all
select * from @testb
--2 join
select isnull(a.id,b.id) as id,isnull(a.a,b.b) as a
from @testa a full join @testb b
on a.id=b.id and a.a=b.b
order by id
--3 exists/in
--3.1 exists
insert into @testa
select * from @testb b where not exists(select 1 from @testa where b.id=id and b.b=a)
select * from @testa
--3.2 in
insert into @testa
select * from @testb b where id not in (select id from @testa where b.id=id and b.b=a)
select * from @testa

--........
angel8601 2006-07-31
  • 打赏
  • 举报
回复
看来还要老夜善于搞这些东东,谢啦,接分

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧