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

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

Sometimes we save a lot of duplicate data in the database table. These duplicate data waste resources. We want to delete them. How should we deal with it? Let’s take a look below.

Let's take a look at our table data first. Some data is repeated.

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 the current result shows the fields with duplicate data in the table.

To delete these duplicate data, we find out the IDs of these data, add the id field in the select statement, and use the max function to get the last id of the duplicate data.

The execution result is shown in the figure, and the ids of the duplicate data are 8 and 9.

In this way, we can use the delete statement to delete the data of these two ids.

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

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

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

It should be noted that if there are three or more rows of duplicate data, we need to execute this statement multiple times, because executing it once will only delete one row in each set of duplicate data.

You may also be interested in:
  • How to delete duplicate records in mysql
  • A complete guide on how to query and delete duplicate records in MySQL
  • Detailed explanation of how to find and delete duplicate data in MySQL and keep only one example
  • Summary of methods for deleting duplicate data in MySQL database
  • MySQL query duplicate data (delete duplicate data and keep the one with the smallest id as the only data)
  • Summary of methods for deleting duplicate records in MySQL database [Recommended]
  • Delete duplicate records of a field in mysql table
  • Shell script to operate MySQL database to delete duplicate data
  • How to delete duplicate rows in mysql
  • MySQL database deletes duplicate data and only retains one method instance

<<:  How to change the dot in the WeChat applet swiper-dot into a slider

>>:  Sample code for implementing Google third-party login in Vue

Recommend

Take you to a thorough understanding of the prototype object in JavaScript

Table of contents 1. What is a prototype? 1.1 Fun...

MySQL Workbench download and use tutorial detailed explanation

1. Download MySQL Workbench Workbench is a graphi...

Detailed explanation of the new features of ES9: Async iteration

Table of contents Asynchronous traversal Asynchro...

Detailed explanation of Vue save automatic formatting line break

I searched for many ways to change it online but ...

Vue implements accordion effect

This article example shares the specific code of ...

jQuery canvas draws picture verification code example

This article example shares the specific code of ...

Tutorial on installing VMWare15.5 under Linux

To install VMWare under Linux, you need to downlo...

MySQL 8.0.11 installation and configuration method graphic tutorial (win10)

This article records the installation and configu...

HTML+CSS+jQuery imitates the search hot list tab effect with screenshots

Copy code The code is as follows: <!DOCTYPE ht...

Tutorial on installing MySQL 8.0.11 using RPM on Linux (CentOS7)

Table of contents 1. Installation preparation 1. ...

Detailed explanation of the principle and function of Vue list rendering key

Table of contents The principle and function of l...

Example of how rem is adapted for mobile devices

Preface Review and summary of mobile terminal rem...

Detailed tutorial on installing mysql 8.0.20 on CentOS7.8

1. Install MySQL software Download and install My...