MySQL query duplicate data (delete duplicate data and keep the one with the smallest id as the only data)

MySQL query duplicate data (delete duplicate data and keep the one with the smallest id as the only data)

Development Background:

Recently, I am working on a function to import batch data into a MySQL database. From the batch import, we can know that such data will not be judged as duplicate before being inserted into the database. Therefore, only after all the data is imported, a statement is executed to delete it to ensure the uniqueness of the data.

Let’s take a look at the detailed introduction.

Actual combat:

The table structure is shown in the following figure:

Indicates: brand

operate:

Use SQL statements to query what duplicate data there are:

SELECT * from brand WHERE brandName IN (
select brandName from brand GROUP BY brandName HAVING COUNT(brandName)>1 #The condition is that the number of duplicate data is greater than 1)

Use SQL to delete redundant duplicate data and keep the unique data with the smallest ID:

Note:

Wrong SQL: DELETE FROM brand WHERE brandName IN (select brandName from brand GROUP BY brandName HAVING COUNT(brandName)>1)
AND Id NOT IN (select MIN(Id) from brand GROUP BY brandName HAVING COUNT(brandName)>1)

Tip: You can't specify target table 'brand' for update in FROM clause

The reason is: the data directly found cannot be used as the condition for deleting data. We should first create a temporary table for the found data, and then use the temporary table as the condition for deletion.

Correct SQL writing:
 DELETE FROM brand WHERE brandName IN (SELECT brandName FROM (SELECT brandName FROM brand GROUP BY brandName HAVING COUNT(brandName)>1) e)
 AND Id NOT IN (SELECT Id FROM (SELECT MIN(Id) AS Id FROM brand GROUP BY brandName HAVING COUNT(brandName)>1) t)

#Querying for duplicate data only displays the first few items, so there is no need to query whether it is the minimum value

The results are as follows:


Summarize:

Many things need to be explored step by step by yourself. Of course, the suggestions on the Internet are also very valuable references and resources. No matter what development we do, we need to understand its working principles in order to better master it.

Well, the above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Find duplicate records in mysql table
  • MySQL SQL statement to find duplicate data based on one or more fields
  • Detailed explanation of how to find and delete duplicate data in MySQL and keep only one example
  • How to query duplicate data in mysql table
  • Summary of methods for deleting duplicate data in MySQL database
  • Very comprehensive MySQL code for handling duplicate data
  • Summary of methods for deleting duplicate records in MySQL database [Recommended]
  • Share the method of ignoring duplicate data when inserting data in MYSQL
  • MySQL uses UNIQUE to implement non-duplicate data insertion
  • A practical record of checking and processing duplicate MySQL records on site

<<:  Implementation of React star rating component

>>:  Linux sudo vulnerability could lead to unauthorized privileged access

Recommend

Detailed explanation of Apache SkyWalking alarm configuration guide

Apache SkyWalking Apache SkyWalking is an applica...

Introduction and usage of Angular pipeline PIPE

Preface PIPE, translated as pipeline. Angular pip...

Talk about the 8 user instincts behind user experience in design

Editor's note: This article is contributed by...

MySQL Flush-List and dirty page flushing mechanism

1. Review The Buffer Pool will be initialized aft...

Summary of some common methods of JavaScript array

Table of contents 1. How to create an array in Ja...

Detailed explanation of incompatible changes of components in vue3

Table of contents Functional Components How to wr...

How to modify the root password of mysql under Linux

Preface The service has been deployed on MySQL fo...

Analyze Mysql transactions and data consistency processing issues

This article analyzes the consistency processing ...

Detailed explanation of MySQL master-slave database construction method

This article describes how to build a MySQL maste...

Solution to using html2canvas to process Dom elements with Baidu map into images

Problem 1: Baidu Map uses tiled images (the map i...

Dealing with the problem of notes details turning gray on web pages

1. In IE, if relative positioning is used, that is...

How to quickly install Nginx in Linux

Table of contents What is nginx 1. Download the r...

MySQL query_cache_type parameter and usage details

The purpose of setting up MySQL query cache is: C...

JS realizes special effects of web page navigation bar

This article shares with you a practical web navi...