MySQL删除多余的重复记录,重复记录只保留一条

CREATE TABLE `white_user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `name` varchar(100) DEFAULT NULL COMMENT '姓名',
  `sex` int(11) DEFAULT NULL COMMENT '性别,0是女生,1是男生',
  `created_time` datetime DEFAULT NULL COMMENT '创建时间',
  `updated_time` datetime DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=36 DEFAULT CHARSET=utf8 COMMENT='白名单表'   

期望:删除多余的name重复的记录,重复name记录只保留一条(最大id的那条):

1:不报错,但不符合期望,把不重复的记录也删除了:


mysql> select * from white_user;
+----+--------+------+---------------------+---------------------+
| id | name   | sex  | created_time        | updated_time        |
+----+--------+------+---------------------+---------------------+
| 30 | 张三   |    1 | 2020-03-18 20:38:57 | 2020-03-18 20:38:57 |
| 31 | 李四   |    1 | 2020-03-18 20:38:57 | 2020-03-18 20:38:57 |
| 32 | 张三   |    1 | 2020-03-18 20:38:57 | 2020-03-18 20:38:57 |
| 33 | 李四   |    1 | 2020-03-18 20:38:57 | 2020-03-18 20:38:57 |
| 34 | 王五   |    0 | 2020-03-18 20:38:57 | 2020-03-18 20:38:57 |
| 35 | 赵六   |    0 | 2020-03-18 20:38:57 | 2020-03-18 20:38:57 |
+----+--------+------+---------------------+---------------------+
6 rows in set (0.00 sec)

mysql> delete from white_user  where  id not in (select max_id from (select max(id) as max_id from white_user  group by name having count(*) >1) b)
    -> ;
Query OK, 4 rows affected (0.00 sec)

mysql> select * from white_user;                                                                                    +----+--------+------+---------------------+---------------------+
| id | name   | sex  | created_time        | updated_time        |
+----+--------+------+---------------------+---------------------+
| 32 | 张三   |    1 | 2020-03-18 20:38:57 | 2020-03-18 20:38:57 |
| 33 | 李四   |    1 | 2020-03-18 20:38:57 | 2020-03-18 20:38:57 |
+----+--------+------+---------------------+---------------------+
2 rows in set (0.00 sec)

 

2 正确:不仅要限制id,还要限制name

mysql> select * from white_user;
+----+--------+------+---------------------+---------------------+
| id | name   | sex  | created_time        | updated_time        |
+----+--------+------+---------------------+---------------------+
| 20 | NULL   |    1 | 2020-03-05 18:34:13 | 2020-03-05 18:34:13 |
| 21 | 李四   | NULL | 2020-03-05 18:34:13 | 2020-03-05 18:34:13 |
| 22 | 王五   |    0 | 2020-03-05 18:34:13 | 2020-03-05 18:34:13 |
| 23 | 赵六   |    0 | 2020-03-05 18:34:13 | 2020-03-05 18:34:13 |
| 24 | 张三   |    1 | 2020-03-05 18:38:10 | 2020-03-05 18:38:10 |
| 25 | 李四   |    1 | 2020-03-05 18:38:10 | 2020-03-05 18:38:10 |
| 26 | 张三   |    1 | 2020-03-05 18:38:10 | 2020-03-05 18:38:10 |
| 27 | 李四   |    1 | 2020-03-05 18:38:10 | 2020-03-05 18:38:10 |
| 28 | 王五   |    0 | 2020-03-05 18:38:10 | 2020-03-05 18:38:10 |
+----+--------+------+---------------------+---------------------+
9 rows in set (0.00 sec)

mysql> delete from white_user  where  id not in (select max_id from (select max(id) as max_id from white_user  group by name having count(*) >1) b)  
 and name in (select name from ( select name from white_user  group by name having count(*) >1 )c) ;
Query OK, 4 rows affected (0.01 sec)

mysql> select * from white_user;                                                                                    +----+--------+------+---------------------+---------------------+
| id | name   | sex  | created_time        | updated_time        |
+----+--------+------+---------------------+---------------------+
| 20 | NULL   |    1 | 2020-03-05 18:34:13 | 2020-03-05 18:34:13 |
| 23 | 赵六   |    0 | 2020-03-05 18:34:13 | 2020-03-05 18:34:13 |
| 26 | 张三   |    1 | 2020-03-05 18:38:10 | 2020-03-05 18:38:10 |
| 27 | 李四   |    1 | 2020-03-05 18:38:10 | 2020-03-05 18:38:10 |
| 28 | 王五   |    0 | 2020-03-05 18:38:10 | 2020-03-05 18:38:10 |
+----+--------+------+---------------------+---------------------+
5 rows in set (0.00 sec)

 

3 虽然1不符合期望,但是1中为何还要用子表b,去掉试试:

 delete from white_user  where  id not in (select max(id) as max_id from white_user  group by name having count(*) >1);
-- ERROR 1093 (HY000): You can't specify target table 'white_user' for update in FROM clause

 

4 2正确,但是为何还要用子表b,c呢?去掉试试:

mysql> delete from white_user  
where
id not in (select max(id) as max_id from white_user  group by name having count(*) >1) 
and name in (select name from white_user  group by name having count(*) >1) ;
-- ERROR 1093 (HY000): You can't specify target table 'white_user' for update in FROM clause

 

你可能感兴趣的:(Mysql)