mysql删除重复记录sql语句
2019/10/10/17:34:38 阅读:1900 来源:谷歌SEO算法 标签:
搜索引擎
我们经常会碰到需要删除数据表中重复记录,下面我来总结了几种能删除重复记录并助相对来讲效率是非常不错的,有需要了解的朋友可进入参考.
创建一个表用来存放,要删除的记录的id信息,代码如下:
- CREATETABLE`tmp_ids`(
- `id`int(11),
- `name`char(20)
- )ENGINE=MyISAM;
如果要删除的记录不多的话,可以把这个表创建成内存表形式,代码如下:
- CREATETABLE`tmp_ids`(
- `id`int(11),
- `name`char(20)
- )ENGINE=HEAP;
然后在test表中删除重复记录,代码如下:
- insertintotmp_idsselectmin(id),namefromtestgroupbynamehavingcount(*)>1orderbynull;
- deletea.*fromtesta,tmp_idsbwhereb.name=a.nameanda.id>b.id;
- truncatetabletmp_ids;
方法二,复制无重复记录到新表格,删除旧表格,然后重命名新表格为旧表名称,代码如下 :
- mysql>select*fromduplicatewhereidin(selectmin(id)fromduplicategroupbyname);
- +----+-------+
- |id|name|
- +----+-------+
- |1|wang|
- |3|wdang|
- |5|wdand|
- |6|wddda|
- +----+-------+
- 4rowsinset(0.01sec)
- mysql>createtableduplicaselect*fromduplicatewhereidin(selectmin(id)fromduplicategroupbyname);
- QueryOK,4rowsaffected(0.02sec)
- Records:4Duplicates:0Warnings:0
- mysql>droptableduplicate;
- QueryOK,0rowsaffected(0.01sec)
- mysql>altertableduplicarenametoduplicate;
- QueryOK,0rowsaffected(0.00sec)
- mysql>select*fromduplicate;
- +----+-------+
- |id|name|
- +----+-------+
- |1|wang|
- |3|wdang|
- |5|wdand|
- |6|wddda|
- +----+-------+
- 4rowsinset(0.00sec)
- --phpfensi.com
- mysql>altertableduplicatemodifyidint(2)notnullprimarykeyauto_increment;
- QueryOK,4rowsaffected(0.00sec)
- Records:4Duplicates:0Warnings:0
后来想了一个语句搞定了,代码如下:
- mysql>usemysql
- Databasechanged
- mysql>select*fromduplicate;
- +----+-------+
- |id|name|
- +----+-------+
- |1|wang|
- |3|wdang|
- |5|wdand|
- |6|wddda|
- |2|wang|
- |4|wdang|
- +----+-------+
- 6rowsinset(0.00sec)
- mysql>deleteduplicateasafromduplicateasa,
- ->(
- ->select*fromduplicategroupbynamehavingcount(1)>1)asb
- ->wherea.name=b.nameanda.id>b.id;
- QueryOK,2rowsaffected(0.00sec)
- mysql>select*fromduplicate;
- +----+-------+
- |id|name|
- +----+-------+
- |1|wang|
- |3|wdang|
- |5|wdand|
- |6|wddda|
- +----+-------+
- 4rowsinset(0.00sec)
保留ID最小的记录.
第1种,代码如下:
- deletefrom%swheregoodsurlin(selectgoodsrulasgurl1from%s
- #groupbygrul1havingcount(gurl1)>1)rs1andidnotin(selectmin(id)asid2from%s
- #groupbygoodsurlhavingcount(goodsurl)>1)rs2"%(a,a,a)
第2种,这种方法不使用子集,但是我不知道怎么把rs1,rs2里面的元组嵌入到SQL语句中,代码如下:
- exeSql="selectmin(id)from%sgroupbygoodsurlhavingcount(goodsurl)>1)"%(a,)--phpfensi.com
- cur.execute(exeSql)
- rs1=cur.fetchall()
- exeSql="selectgoodsurlfrom%sgroupbygoodsurlhavingcount(goodsurl)>1"%(a,)
- cur.execute(exeSql)
- rs2=cur.fetchall()
- exeSql="deletefrom%swheregoodsurlin%sandidnotin%"%(a,rs2,rs1)
- cur.execute(exeSql)
热门评论