SQL数据库删除重复行保留一条记录
SQL数据库删除重复行保留一条记录
看你的表结构,我简单写一条你参考一下:
Delete From table
where Id not in (Select min(id),column1,column2 From table group by column1,column2)
删除重复,保留最小id
delete from table where exists (select 1 from table t1 where t1.重复字段 = table.重复字段 and t1.id < table.id )
-----------看成是一个循环,从表第一行到最后一行,如果存在比本行ID小的重复数据,那么删除本行。这样循环后的最终结果就是保留了最小ID的一行。
保留最大也是同理
delete from table where exists (select 1 from table t1 where t1.重复字段 = table.重复字段 and t1.id > table.id )
---------一个循环,从第一行到最后一行,如果存在ID比本行大的重复数据,删除本行,最后剩下的都是不重复的且ID是最大的记录。
http://zhidao.baidu.com/question/264532970.html?qbl=relate_question_0&word=sql%C9%BE%B3%FD%D6%D8%B8%B4%BC%C7%C2%BC%B1%A3%C1%F4%D2%BB%CC%F5
浙公网安备 33010602011771号