如何返回多字段重复的完整记录集
id a b c d
1 zh tec 21 null
2 ze tec 32 bb
3 ze tec 37 null
4 ze tec 37 cc
5 ze te 22 bb
6 zh tec 21 cc
7 zh te 32 null
id为标识列。
1. 查询:要找出列a,b都相同的完整记录集(后面需要判断列c和列d):
id a b c d
1 zh tec 21 null
6 zh tec 21 cc
2 ze tec 32 bb
3 ze tec 37 null
4 ze tec 37 cc
2. 删除:对于列a,b都相同的记录,只保留列d不为空值且id最大的记录,即删除以下记录:
id a b c d
1 zh tec 21 null
2 ze tec 32 bb
3 ze tec 37 null
[解决办法]
delete from T
where id in(
select id from T t
where d is null and
exists(select 1 from T
where t.a = a and t.b = b and t.id > id))
[解决办法]
select * from tb as m,(select a ,bfrom tbgroup by a , bhaving count(*)> 1)nwhere m.a = n.a and m.b = n.b
[解决办法]
问题2删除:delete t1 from tb t1 where exists(select 1 from tb t2 where t2.d is not null and t1.a =t2.a and t1.b =t2.b and t1.id<t2.id)