SQL code:
declare @tb1 table(a varchar(20),b varchar(20))
insert into @tb1 select 'a101','b11121'
insert into @tb1 select 'a102','b00012'
select * from @tb1
declare @tb2 table(id int,c varchar(20))
insert into @tb2 select 1,'a101'
insert into @tb2 select 2,'b11121'
insert into @tb2 select 3,'a000'
insert into @tb2 select 4,'c1001'
select * from @tb2
我现在要把 @tb2 中c字段 在 @tb1 中有重复的数据删除,保留@tb2 中id 较小的
如
@tb1中 'a101',' b11121' 在同一行,
而@tb2 中c字段有
1 a101
2 b11121
3 a000
4 c1001
就删除第二行,最后得到结果
1 a101
3 a000
4 c1001
这个sql怎么写?
修改一下
SQL code:
declare @tb1 table(id int,a varchar(20),b varchar(20))
insert into @tb1 select 1,'a101','b11121'
insert into @tb1 select 2,'a102','b00012'
select * from @tb1
declare @tb2 table(id int,c varchar(20))
insert into @tb2 select 1,'a101'
insert into @tb2 select 2,'b11121'
insert into @tb2 select 3,'a000'
insert into @tb2 select 4,'c1001'
select * from @tb2
两表id都是唯一的
我现在要把 @tb2 中c字段 在 @tb1 中有重复的数据删除,保留@tb2 中id 较小的
如
@tb1中 'a101',' b11121' 在同一行,
而@tb2 中c字段有
1 a101
2 b11121
3 a000
4 c1001
就删除第二行,最后得到结果
1