Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summary of mysql methods for finding and deleting duplicate data in tables

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

In the database table, we sometimes save a lot of duplicate data, which is a waste of resources. What should we do if we want to delete it? Let's take a look.

Let's take a look at our table data first. Some of the data are duplicated.

To find duplicate data, we can use the having statement in mysql, as shown in the figure.

After executing this statement, we can see that what is now shown in the result is the field of duplicate data in the table.

To delete the duplicated data, we find the ID of the data, add the id field in the select statement, and use the max function to get the id at the end of the duplicated data.

The execution result is shown in the figure, and the id of the duplicated data is 8 and 9.

So we can use the delete statement to delete the data for these two id.

But what if there is a lot of duplicate data and you don't want to write these id one by one?

Add a subquery and query only the id field, as shown in the figure.

Then add a delete statement outside, the detailed code is shown in the figure.

It is important to note that if there are three or more rows of duplicate data, we need to execute this statement multiple times, because only one of each set of duplicate data will be deleted at once.

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report