记录集合并问题?在线等!

yalin520 2008-04-08 05:09:11
记录集A
userid pubcount
aa 10
bb 2
cc 3
记录集B
userid recount
cc 8
dd 9

记录集C
userid pubcount recount
aa 10 0
bb 2 0
cc 3 8
dd 0 9
把记录集A与记录集B 合并成记录集C ,sql 怎样合并?
...全文
132 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yalin520 2008-04-08
  • 打赏
  • 举报
回复
呵呵呵 哥们些还真有趣!
青锋-SS 2008-04-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yalin520 的回复:]
引用 4 楼 yalin520 的回复:
呵呵 谢谢楼上的哥们些,问题解决了
不得不承认 我的SQL技术太菜了 谢谢各位指点


啥不公?
[/Quote]你把分平分或者全给他就公了.
yalin520 2008-04-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yalin520 的回复:]
呵呵 谢谢楼上的哥们些,问题解决了
不得不承认 我的SQL技术太菜了 谢谢各位指点
[/Quote]

啥不公?
wzy_love_sly 2008-04-08
  • 打赏
  • 举报
回复
我的速度快
wzy_love_sly 2008-04-08
  • 打赏
  • 举报
回复
nnd 不公啊
yalin520 2008-04-08
  • 打赏
  • 举报
回复
呵呵 谢谢楼上的哥们些,问题解决了
不得不承认 我的SQL技术太菜了 谢谢各位指点
wzy_love_sly 2008-04-08
  • 打赏
  • 举报
回复
declare @a table (userid varchar(10),pubcount int)
insert into @a select 'aa',10
insert into @a select 'bb',2
insert into @a select 'cc',3
declare @b table (userid varchar(10),recount int)
insert into @b select 'cc',8
insert into @b select 'dd',9

select tp.userid,isnull(a.pubcount,0) as pubcount ,isnull(b.recount,0) as recount
from (select distinct userid from @a union select distinct userid from @b) tp left join @a a on tp.userid=a.userid
left join @b b on tp.userid=b.userid



userid pubcount recount
aa 10 0
bb 2 0
cc 3 8
dd 0 9
青锋-SS 2008-04-08
  • 打赏
  • 举报
回复
create table a(userid  varchar(20),pubcount int)
go
insert into a select ' aa', 10
insert into a select 'bb', 2
insert into a select 'cc', 3
go
create table B(userid varchar(20), recount int)
go
insert into b select 'cc', 8
insert into b select 'dd', 9
go
select * from a
select * from b
select isnull(a.userid,b.userid) as userid,isnull(a.pubcount,0) as pubcount,isnull(b.recount,0) as recount
from a full join b
on a.userid=b.userid
go
drop table a,b
go


(1 行受影响)

(1 行受影响)

(1 行受影响)

(1 行受影响)

(1 行受影响)
userid pubcount
-------------------- -----------
aa 10
bb 2
cc 3

(3 行受影响)

userid recount
-------------------- -----------
cc 8
dd 9

(2 行受影响)

userid pubcount recount
-------------------- ----------- -----------
aa 10 0
bb 2 0
cc 3 8
dd 0 9

(4 行受影响)

青锋-SS 2008-04-08
  • 打赏
  • 举报
回复
select isnull(a.userid,b.userid) as userid,isnull(a.pubcount,0) as pubcount,isnull(b.recount,0) as recount
from a full join b
on a.userid=b.userid

34,590

社区成员

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

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