日期:2014-05-18  浏览次数:20498 次

两表不同数据区别sqlserver语句
aaa表结构 id 缴费单位 总额
bbb表结构 id 缴费单位 总额
 aaa表数据如下
缴费单位 总额 id
和信致远建筑工程有限公司 ¥5,630.00 1
年嘉达气体有限公司 ¥2,300.00 2
年嘉达气体有限公司 ¥2,660.00 3
旭龙弹簧厂 ¥3,260.00 4
歌纳卫浴有限公司 ¥3,695.00 5
旭硝子汽车玻璃有限公司 ¥300.00 6
南方方法实业有限公司 ¥57,246.00 7
东南灯饰照明有限公司 ¥6,557.00 8
云雀振动器有限公司 ¥5,200.00 9
华通混凝土有限公司 ¥7,257.00 10
华通混凝土有限公司 ¥7,257.00 11


bbb表数据如下
缴费单位 总额 id
年嘉达气体有限公司 ¥2,300.00 26
年嘉达气体有限公司 ¥2,660.00 36
旭龙弹簧厂 ¥3,260.00 42
歌纳卫浴有限公司 ¥3,695.00 58
旭硝子汽车玻璃有限公司 ¥300.00 76
南方方法实业有限公司 ¥57,246.00 17
东南灯饰照明有限公司 ¥6,557.00 80
云雀振动器有限公司 ¥5,200.00 93
华通混凝土有限公司 ¥7,257.00 110
 说明:bbbb表数据为aaa表的子集
现在要得出aaa表有而在bbb表没有的记录
如上例要求得到如下结果:
和信致远建筑工程有限公司 ¥5,630.00  
华通混凝土有限公司 ¥7,257.00

------解决方案--------------------
SQL code

declare @aaa table 
(缴费单位 varchar(24),总额 VARCHAR(30),id  int)
insert into @aaa
select '和信致远建筑工程有限公司','¥5,630.00',1 union all
select '年嘉达气体有限公司','¥2,300.00',2 union all
select '年嘉达气体有限公司','¥2,660.00',3 union all
select '旭龙弹簧厂','¥3,260.00',4 union all
select '歌纳卫浴有限公司','¥3,695.00',5 union all
select '旭硝子汽车玻璃有限公司','¥300.00',6 union all
select '南方方法实业有限公司','¥57,246.00',7 union all
select '东南灯饰照明有限公司','¥6,557.00',8 union all
select '云雀振动器有限公司','¥5,200.00',9 union all
select '华通混凝土有限公司','¥7,257.00',10 union all
select '华通混凝土有限公司','¥7,257.00',11

declare @bbb table 
(缴费单位 varchar(22),总额 VARCHAR(30),id  int)
insert into @bbb
select '年嘉达气体有限公司','¥2,300.00',26 union all
select '年嘉达气体有限公司','¥2,660.00',36 union all
select '旭龙弹簧厂','¥3,260.00',42 union all
select '歌纳卫浴有限公司','¥3,695.00',58 union all
select '旭硝子汽车玻璃有限公司','¥300.00',76 union all
select '南方方法实业有限公司','¥57,246.00',17 union all
select '东南灯饰照明有限公司','¥6,557.00',80 union all
select '云雀振动器有限公司','¥5,200.00',93 union all
select '华通混凝土有限公司','¥7,257.00',110

select a.* from  @aaa a 
left join @bbb b on a.缴费单位=b.缴费单位 where b.id is NULL

/*
缴费单位                     总额                             id
------------------------ ------------------------------ -----------
和信致远建筑工程有限公司             ¥5,630.00                      1
*/

--我只能得到一条数据,另外那条的规则是什么?

------解决方案--------------------
SQL code

select * from aaa a
where a.缴费单位 not in(select 缴费单位 from bbb)
或者exists not(据说效率比not in高)(具体用法百度下,再自己实践,你会有收获的)

------解决方案--------------------
SQL code
--aaa表结构 id 缴费单位 总额
--bbb表结构 id 缴费单位 总额

 if OBJECT_ID('aaa') is not null 
 drop table aaa 
 go 
 create table aaa (缴费单位 varchar(50),总额 varchar(50),id int)
  insert into aaa
 select '和信致远建筑工程有限公司', '¥5,630.00', 1  union all
 select '年嘉达气体有限公司', '¥2,300.00', 2   union all
 select '年嘉达气体有限公司', '¥2,660.00', 3union all
 select'旭龙弹簧厂' ,'¥3,260.00', 4 union all
select'歌纳卫浴有限公司' ,'¥3,695.00', 5 union all
select'旭硝子汽车玻璃有限公司' ,'¥300.00', 6 union all
select'南方方法实业有限公司' ,'¥57,246.00', 7 union all
select'东南灯饰照明有限公司' ,'¥6,557.00', 8 union all
select'云雀振动器有限公司', '¥5,200.00' ,9 union all
select'华通混凝土有限公司' ,'¥7,257.00', 10 union all
select'华通混凝土有限公司', '¥7,257.00', 11  



 if OBJECT_ID('bbb') is not null 
 drop table bbb 
 go 
 create table bbb (缴费单位 varchar(50),总额 varchar(50),id int)
  insert into bbb
 select'年嘉达气体有限公司', '¥2,300.00', 26 union all
select'年嘉达气体有限公司 ','¥2,660.00', 36 union all
select'旭龙弹簧厂' ,'¥3,260.00', 42 union all
select'歌纳卫浴有限公司' ,'¥3,695.00', 58 union all
select'旭硝子汽车玻璃有限公司' ,'¥300.00' ,76 uni