文章详细的利用了一个实例来告诉我们如何在mysql删除数据库重复记录的实现方法与原理,有需要的同学可以参考一下.
演示数据,表结构,代码如下:
- mysql>descdemo;
- +-------+------------------+------+-----+---------+----------------+
- |Field|Type|Null|Key|Default|Extra|
- +-------+------------------+------+-----+---------+----------------+
- |id|int(11)unsigned|NO|PRI|NULL|auto_increment|
- |site|varchar(100)|NO|MUL|||
- +-------+------------------+------+-----+---------+----------------+
- 2rowsinset(0.00sec)
- --数据:
- mysql>select*fromdemoorderbyid;
- +----+------------------------+
- |id|site|
- +----+------------------------+
- |1|http://www.phpfensi.com |
- |2||
- |3|http://www.phpfensi.com |
- |4|http://www.phpfensi.com |
- |5|http://www.phpfensi.com |
- +----+------------------------+
- 5rowsinset(0.00sec)
当没有创建表或创建索引权限的时候,如果你要删除较旧的重复记录,可以使用下面的语句:
- mysql>deletefroma
- ->usingdemoasa,demoasb
- ->where(a.id>b.id)
- ->and(a.site=b.site);
- QueryOK,2rowsaffected(0.12sec)
- mysql>select*fromdemoorderbyid;
- +----+------------------------+
- |id|site|
- +----+------------------------+
- |1|http://www.phpfensi.com |
- |2||
- |3|http://www.phpfensi.com |
- +----+------------------------+
- 3rowsinset(0.00sec)
你要删除较新的重复记录,可以使用下面的语句:
- mysql>deletefroma
- ->usingdemoasa,demoasb
- ->where(a.id<b.id)
- ->and(a.site=b.site);
- QueryOK,2rowsaffected(0.12sec)
- mysql>select*fromdemoorderbyid;
- +----+------------------------+
- |id|site|
- +----+------------------------+
- |2||
- |4|http://www.phpfensi.com |
- |5|http://www.phpfensi.com |
- +----+------------------------+
- 3rowsinset(0.00sec)
你可以用下面的语句先确认将被删除的重复记录,代码如下:
- mysql>SELECTa.*
- ->FROMdemoa,demob
- ->WHEREa.id>b.id
- ->AND(a.site=b.site);
- +----+------------------------+
- |id|site|
- +----+------------------------+
- |1|http://www.phpfensi.com |
- |3|http://www.phpfensi.com |
- +----+------------------------+
- 2rowsinset(0.00sec)
如果有创建索引的权限,可以用下面的方法,在表上创建唯一键索引,代码如下:
- mysql>alterignoretabledemoadduniqueindexukey(site);
- QueryOK,5rowsaffected(0.46sec)
- Records:5Duplicates:2Warnings:0
- mysql>select*fromdemoorderbyid;
- +----+------------------------+
- |id|site|
- +----+------------------------+
- |1|http://www.phpfensi.com |
- |2||
- |3|http://www.phpfensi.com |
- +----+------------------------+
- 3rowsinset(0.00sec)
重复记录被删除后,如果需要,可以删除索引,代码如下:
- mysql>altertabledemodropindexukey;
- QueryOK,3rowsaffected(0.37sec)
- Records:3Duplicates:0Warnings:0
如果有创建表的权限,可以用下面的方法,创建一个新表,然后将原表中不重复的数据插入新表,代码如下:
- mysql>createtabledemo_newasselect*fromdemogroupbysite;
- QueryOK,3rowsaffected(0.19sec)
- Records:3Duplicates:0Warnings:0
- mysql>showtables;
- +----------------+
- |Tables_in_test|
- +----------------+
- |demo|
- |demo_new|
- +----------------+
- 2rowsinset(0.00sec)
- mysql>select*fromdemoorderbyid;
- +----+------------------------+
- |id|site|
- +----+------------------------+
- |1|http://www.phpfensi.com |
- |2||
- |3|http://www.phpfensi.com |
- |4|http://www.phpfensi.com |
- |5|http://www.phpfensi.com |
- +----+------------------------+
- 5rowsinset(0.00sec)
- mysql>select*fromdemo_neworderbyid;
- +----+------------------------+
- |id|site|
- +----+------------------------+
- |1|http://www.phpfensi.com |
- |2||
- |3|http://www.phpfensi.com |
- +----+------------------------+
- 3rowsinset(0.00sec)
然后将原表备份,将新表重命名为当前表,代码如下:
- mysql>renametabledemotodemo_old,demo_newtodemo;
- QueryOK,0rowsaffected(0.04sec)
- mysql>showtables;
- +----------------+
- |Tables_in_test|
- +----------------+
- |demo|
- |demo_old|
- +----------------+
- 2rowsinset(0.00sec)
- mysql>select*fromdemoorderbyid;
- +----+------------------------+
- |id|site|
- +----+------------------------+
- |1|http://www.phpfensi.com |
- |2||
- |3|http://www.phpfensi.com |
- +----+------------------------+
- 3rowsinset(0.00sec)
注意:使用这种方式创建的表会丢失原表的索引信息,代码如下:
- mysql>descdemo;
- +-------+------------------+------+-----+---------+-------+
- |Field|Type|Null|Key|Default|Extra|
- +-------+------------------+------+-----+---------+-------+
- |id|int(11)unsigned|NO||0||
- |site|varchar(100)|NO||||
- +-------+------------------+------+-----+---------+-------+
- 2rowsinset(0.00sec)
如果要保持和原表信息一致,你可以使用 show create table demo;来查看原表的创建语句,然后使用原表的创建语句创建新表,接着使用 insert … select 语句插入数据,再重命名表即可.