清除冗余记录的SQL语句

类别:数据库 点击:0 评论:0 推荐:

问题:设表T中有五个字段A,B,C,D,ID,其中ID字段是为自动增量整型字段(唯一值)。业务数据生成时有重复插入现象,现需对A、B、C、D四字段完全一样的脏数据进行清理,要求设计一SQL语句,对四个字段完全一样的记录,仅保留ID最小的一条,其余作删除处理。

DELETE FROM T WHERE [ID] IN
(
 select [ID] from T where A in
  (select A  from T Group By A,B,C,D Having count(*)>1)
 and [ID]<>
  (select min(ID) as minID  from T Group By A,B,C,D Having count(*)>1)
)

更正:
delete from T
where T.ID not in (select min(ID) from T group by T.A,T.B,T.C,T.D)

本文地址:http://com.8s8s.com/it/it19754.htm