码迷,mamicode.com
首页 > 数据库 > 详细

mysql去除重复数据

时间:2018-01-11 20:37:56      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:null   inf   图片   多个   最小   数据还原   根据   ima   结果   

今天一个同学问我mysql去除重复数据,自己做了个测试顺便记录下:

查看表结构:

mysql> desc testdelete;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| one   | varchar(40) | YES  |     | NULL    |                |
| two   | varchar(40) | YES  |     | NULL    |                |
| three | varchar(40) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.10 sec)

 

 

表的数据:

mysql> select * from testdelete;
+----+------+------+-------+
| id | one  | two  | three |
+----+------+------+-------+
|  1 | A    | A    | A     |
|  2 | B    | B    | B     |
|  3 | C    | C    | C     |
|  4 | D    | D    | D     |
|  5 | E    | E    | E     |
|  6 | A    | A    | B     |
| 12 | A    | A    | A     |
| 13 | A    | A    | A     |
| 14 | A    | A    | A     |
| 15 | A    | A    | A     |
+----+------+------+-------+
10 rows in set (0.00 sec)

 

 

 

接下来进行测试:

1.根据one列查询重复的数据(根据单列判断重复)

SELECT * FROM testdelete 
WHERE ONE IN (SELECT ONE FROM testdelete GROUP BY ONE HAVING COUNT(ONE) > 1) 

 

 

结果:

技术分享图片

 

 2.删除表中的重复记录:(根据单列删除且保留ID最小的一条

DELETE
FROM testdelete
WHERE ONE IN(SELECT
               ONE
             FROM testdelete
             GROUP BY ONE
             HAVING COUNT(ONE) > 1)
    AND id NOT IN(SELECT
                    MIN(id)
                  FROM testdelete
                  GROUP BY ONE
                  HAVING COUNT(ONE) > 1)

 

 

 报错:

技术分享图片

 

原因:大概是因为不能直接在查询的语句中进行操作。

解决办法:将查询包装一层:

DELETE
FROM testdelete
WHERE ONE IN(SELECT
               ONE
             FROM (SELECT
                     ONE
                   FROM testdelete
                   GROUP BY ONE
                   HAVING COUNT(ONE) > 1) a)
    AND id NOT IN(SELECT
                    *
                  FROM (SELECT
                          MIN(id)
                        FROM testdelete
                        GROUP BY ONE
                        HAVING COUNT(ONE) > 1) b)

 

 

 结果:

(5 row(s) affected)
Execution Time : 00:00:00:094
Transfer Time : 00:00:00:000
Total Time : 00:00:00:094

 

再次查看数据:

技术分享图片

 

 

将数据还原。 

 

 3.根据one,two,three判断重复:(根据单多判断重复)

SELECT * FROM testdelete a 
WHERE (a.one,a.two,a.three) IN (SELECT ONE,two,three FROM testdelete GROUP BY ONE,two,three HAVING COUNT(*) > 1) 

 

结果;

技术分享图片

 

 

4.删除表中的重复数据(根据多列进行删除且保留ID最小的一条)

 

DELETE
FROM testdelete
WHERE (ONE,two,three)IN(SELECT
                          ONE,
                          two,
                          three
                        FROM (SELECT
                                ONE,
                                two,
                                three
                              FROM testdelete
                              GROUP BY ONE,two,three
                              HAVING COUNT( * ) > 1) a)
    AND id NOT IN(SELECT
                    MIN(id)
                  FROM (SELECT
                          MIN(id) AS id
                        FROM testdelete
                        GROUP BY ONE,two,three
                        HAVING COUNT( * ) > 1) b)

 

 

 结果:

(4 row(s) affected)
Execution Time : 00:00:00:125
Transfer Time : 00:00:00:000
Total Time : 00:00:00:125

 

查看数据:

技术分享图片

 

 

数据还原

 5. 查找表中多余的重复记录(多个字段),不包含id最小的记录 (根据多个字段查重复不包含id最小的)

SELECT *
FROM testdelete a
WHERE (a.one,a.two,a.three)IN(SELECT
                                ONE,
                                two,
                                three
                              FROM testdelete
                              GROUP BY ONE,two,three
                              HAVING COUNT( * ) > 1)
    AND id NOT IN(SELECT
                    MIN(id) AS id
                  FROM testdelete
                  GROUP BY ONE,two,three
                  HAVING COUNT( * ) > 1)

 

 结果:

技术分享图片

 

mysql去除重复数据

标签:null   inf   图片   多个   最小   数据还原   根据   ima   结果   

原文地址:https://www.cnblogs.com/qlqwjy/p/8270011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!