A表:
姓名 成绩 性别
一 10 男
二 20 女
三 15 男
表B
姓名 成绩 性别
一 10 男
二 20 女
三 15 男
四 20 女
八 20 女
我的问题是:比对2张表,从中挑出不一样的数据,并产生一张新表!谢谢!
姓名 成绩 性别
一 10 男
二 20 女
三 15 男
表B
姓名 成绩 性别
一 10 男
二 20 女
三 15 男
四 20 女
八 20 女
我的问题是:比对2张表,从中挑出不一样的数据,并产生一张新表!谢谢!
from tb b
where not exists(select 1 from ta where checksum(姓名,成绩, 性别) = checksum(b.姓名,b.成绩, b.性别)
(
select * from A where checksum(*) not in (select checksum(*) from B)
union all
select * from B where checksum(*) not in (select checksum(*) from A)
) t
select [姓名] ,[成绩] ,[性别]
from
(
select * from a union all select * from b
) v
group by [姓名] ,[成绩] ,[性别]
having count(1)=1
from tb b
where not exists(select 1 from ta where checksum(姓名,成绩, 性别) = checksum(b.姓名,b.成绩, b.性别)
coalesce(a.成绩,b.成绩) 成绩,
coalesce(a.性别,b.性别) 性别
from a full join b
on checksum(a.姓名,a.成绩,a.性别) = checksum(b.姓名,b.成绩, b.性别)
union all
select * from #B except select * from #A
union all
select b.* from b except select a.* from a /*找出b表中有的数据a表中没有的数据*/
into C
from A, B
where A.姓名 <> B.姓名 and A.成绩 = B.成绩 and A.性别 = B.性别
create table A(
姓名 nvarchar(5)
,成绩 int
,性别 nvarchar(1)
)
goinsert into A
select '一',10,'男' union all
select '二',20,'女' union all
select '三',15,'男'
gocreate table B(
姓名 nvarchar(5)
,成绩 int
,性别 nvarchar(1)
)
goinsert into B
select '一',10,'男' union all
select '二',20,'女' union all
select '三',20,'男' union all
select '四',20,'女' union all
select '八',20,'女'
goselect 'A中有而B中没有的姓名', A.* from A left join B on a.姓名 = b.姓名 where b.姓名 is null
union all
select 'B中有而A中没有的姓名', B.* from B left join A on a.姓名 = b.姓名 where a.姓名 is null
from
(
select * from A union all select * from B
) v
group by 姓名,成绩,性别
having count(1)=1
union all
(select #A.* from #A except select #B.* from #B )
into newtable
from (
select * from a
union
select * from b) as a
from tb b
where not exists(select 1 from ta where checksum(姓名,成绩, 性别) = checksum(b.姓名,b.成绩, b.性别)