mysql中删除重复记录的方案
发布时间:2022-02-11 17:25:46 所属栏目:MySql教程 来源:互联网
导读:这篇文章主要讲解了mysql中删除重复记录的方法,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习mysql中删除重复记录的方法吧! 用临时过渡表,这种很笨拙的方法。 如果数据库的设计不规范,某个表没有主健,
这篇文章主要讲解了“mysql中删除重复记录的方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“mysql中删除重复记录的方法”吧! 用临时过渡表,这种很笨拙的方法。 如果数据库的设计不规范,某个表没有主健,那么肯定会有重复记录的现象,或者存在这种危险。在oracle中,可以通过rowid来删除重复记录。但在mysql中如何实现呢? google一下,有个方法如下: 1、准备基表test,待测试。 create table test (c1 smallint,c2 smallint); insert into test values(1,1); insert into test values(1,1); insert into test values(1,2); insert into test values(2,2); insert into test values(2,2); 2、创建一个临时表,结构与原表一致,但没有数据。 create table tmp as select * from test where 1=2; 3、将原表数据插入临时表,重复记录合为一条记录。 insert into tmp select distinct * from test; 4、查看一下基表和临时表的数据。 mysql> select * from test; +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | | 1 | 1 | | 1 | 2 | | 2 | 2 | | 2 | 2 | +------+------+ 5 rows in set (0.00 sec) mysql> select * from tmp; +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | | 1 | 2 | | 2 | 2 | +------+------+ 3 rows in set (0.00 sec) 5、删除基表数据 mysql> delete from test; Query OK, 5 rows affected (0.02 sec) 6、将临时表数据插回基表 mysql> insert into test select * from tmp; Query OK, 3 rows affected (0.03 sec) Records: 3 Duplicates: 0 Warnings: 0 7、查看一下基表的数据 mysql> select * from test; +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | | 1 | 2 | | 2 | 2 | +------+------+ 3 rows in set (0.00 sec) 8、数据准确,完成基表重复记录的删除。 感谢各位的阅读,以上就是“mysql中删除重复记录的方法”的内容了,经过本文的学习后,相信大家对mysql中删除重复记录的方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。 (编辑:晋中站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |