How to delete table duplicate records in MySQL
This article is to share with you about how to delete table duplicate records in MySQL, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Create a lab table student table data:
Mysql > use test
Database changed
Mysql > create table student (id int,name varchar (10))
Query OK, 0 rows affected (1.67 sec)
Mysql > insert into student values (11)
Query OK, 1 row affected (0.26 sec)
Mysql > insert into student values (12)
Query OK, 1 row affected (0.07 sec)
Mysql > insert into student values (13 ~ (14) Ma)
Query OK, 1 row affected (0.12 sec)
Mysql > insert into student values (14)
Query OK, 1 row affected (0.11 sec)
Mysql > insert into student values (15Chinese BBB')
Query OK, 1 row affected (0.19 sec)
Mysql > insert into student values (16 ~ (th))
Query OK, 1 row affected (0.14 sec)
Mysql > insert into student values (17meme cc')
Query OK, 1 row affected (0.15 sec)
Mysql > select * from student
+-+ +
| | id | name |
+-+ +
| | 11 | aa |
| | 12 | aa |
| | 13 | aa |
| | 14 | aa |
| | 15 | bb |
| | 16 | bb |
| | 17 | cc |
+-+ +
7 rows in set (0.22 sec)
Method 1:
Mysql > create temporary table temp as select min (id), name from student group by name
Query OK, 3 rows affected (0.18 sec)
Records: 3 Duplicates: 0 Warnings: 0
Mysql > truncate table student
Query OK, 0 rows affected (0.40 sec)
Mysql > insert into student select * from temp
Query OK, 3 rows affected (0.11 sec)
Records: 3 Duplicates: 0 Warnings: 0
Mysql > select * from student
+-+ +
| | id | name |
+-+ +
| | 11 | aa |
| | 15 | bb |
| | 17 | cc |
+-+ +
3 rows in set (0.00 sec)
Mysql > drop temporary table temp
Query OK, 0 rows affected (0.17 sec)
Method 2:
Mysql > create temporary table temp as select min (id) as MINID from student group by name
Query OK, 3 rows affected (0.24 sec)
Records: 3 Duplicates: 0 Warnings: 0
Mysql > delete from student where id not in (select minid from temp)
Query OK, 4 rows affected (0.07 sec)
Mysql > select * from student
+-+ +
| | id | name |
+-+ +
| | 11 | aa |
| | 15 | bb |
| | 17 | cc |
+-+ +
3 rows in set (0.00 sec)
Method 3:
Mysql > delete from student where id not in (select minid from (select min (id) as minid from student)
Group by name) b)
Query OK, 4 rows affected (0.19 sec)
Mysql > select * from student
+-+ +
| | id | name |
+-+ +
| | 11 | aa |
| | 15 | bb |
| | 17 | cc |
+-+ +
3 rows in set (0.00 sec)
The above is how to delete table duplicate records in MySQL. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.