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:
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:
|
<<: Implementation of React star rating component
>>: Linux sudo vulnerability could lead to unauthorized privileged access
Apache SkyWalking Apache SkyWalking is an applica...
Preface PIPE, translated as pipeline. Angular pip...
Editor's note: This article is contributed by...
1. Review The Buffer Pool will be initialized aft...
Table of contents 1. How to create an array in Ja...
Table of contents Functional Components How to wr...
Preface The service has been deployed on MySQL fo...
This article analyzes the consistency processing ...
This article describes how to build a MySQL maste...
Problem 1: Baidu Map uses tiled images (the map i...
1. In IE, if relative positioning is used, that is...
WebRTC, which stands for Web Real-Time Communicat...
Table of contents What is nginx 1. Download the r...
The purpose of setting up MySQL query cache is: C...
This article shares with you a practical web navi...