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

MySQL 5.7.18 free installation version window configuration method

This is my first blog. It’s about when I started ...

How to use Dockerfile to build images in Docker

Build the image Earlier we used various images fo...

Detailed explanation of MYSQL stored procedure comments

Table of contents 1. Instructions for use 2. Prep...

VMware Workstation is not compatible with Device/Credential Guard

When installing a virtual machine, a prompt appea...

Detailed explanation of Linux command unzip

Table of contents 1. unzip command 1.1 Syntax 1.2...

Detailed explanation of Java calling ffmpeg to convert video format to flv

Detailed explanation of Java calling ffmpeg to co...

Robots.txt detailed introduction

Robots.txt is a plain text file in which website ...

MySQL 8.0.11 compressed version installation tutorial

This article shares the installation tutorial of ...

Vue+element ui realizes anchor positioning

This article example shares the specific code of ...

If I change a property randomly in Vue data, will the view be updated?

Interviewer: Have you read the source code of Vue...

Implementation of automatic completion of Docker commands

Preface I don't know how long this friend has...

A brief analysis of React's understanding of state

How to define complex components (class component...

Definition and usage of MySQL cursor

Creating a Cursor First, create a data table in M...