In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.