如何查找和删除数据库中的重复数据?

2024-11-29 01:56:49
推荐回答(1个)
回答1:

法一: 用Group by语句 此查找很快的select count(num), max(name) from student –查找表中num列重复的,列出重复的记录数,并列出他的name属性group by numhaving count(num) >;1 –按num分组后找出表中num列重复,即出现次数大于一次delete from student(上面Select的)这样的话就把所有重复的都删除了。—–慎重法二:当表比较大(例如10万条以上)时,这个方法的效率之差令人无法忍受,需要另想办法:—- 执行下面SQL语句后就可以显示所有DRAWING和DSNO相同且重复的记录SELECT * FROM EM5_PIPE_PREFABWHERE ROWID!=(SELECT MAX(ROWID) FROM EM5_PIPE_PREFAB D –D相当于First,SecondWHERE EM5_PIPE_PREFAB.DRAWING=D.DRAWING ANDEM5_PIPE_PREFAB.DSNO=D.DSNO);—- 执行下面SQL语句后就可以删除所有DRAWING和DSNO相同且重复的记录DELETE FROM EM5_PIPE_PREFABWHERE ROWID!=(SELECT MAX(ROWID) FROM EM5_PIPE_PREFAB DWHERE EM5_PIPE_PREFAB.DRAWING=D.DRAWING ANDEM5_PIPE_PREFAB.DSNO=D.DSNO);法一: 用Group by语句 此查找很快的select count(num), max(name) from student –查找表中num列重复的,列出重复的记录数,并列出他的name属性group by numhaving count(num) >;1 –按num分组后找出表中num列重复,即出现次数大于一次delete from student(上面Select的)这样的话就把所有重复的都删除了。—–慎重法二:当表比较大(例如10万条以上)时,这个方法的效率之差令人无法忍受,需要另想办法:—- 执行下面SQL语句后就可以显示所有DRAWING和DSNO相同且重复的记录SELECT * FROM EM5_PIPE_PREFABWHERE ROWID!=(SELECT MAX(ROWID) FROM EM5_PIPE_PREFAB D –D相当于First,SecondWHERE EM5_PIPE_PREFAB.DRAWING=D.DRAWING ANDEM5_PIPE_PREFAB.DSNO=D.DSNO);—- 执行下面SQL语句后就可以删除所有DRAWING和DSNO相同且重复的记录DELETE FROM EM5_PIPE_PREFABWHERE ROWID!=(SELECT MAX(ROWID) FROM EM5_PIPE_PREFAB DWHERE EM5_PIPE_PREFAB.DRAWING=D.DRAWING ANDEM5_PIPE_PREFAB.DSNO=D.DSNO);