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
Last time, a very studious fan asked if it was po...
There are many tools available for backing up MyS...
Hexadecimal code table of various colors [Part 1] ...
As the number of visits increases, the pressure o...
This article shares the specific code of javascri...
Vulnerability Description Apache Flink is an open...
Effect picture: html: <div class='site_bar...
Update: Recently, it was discovered that the serv...
1. Docker Network Mode When docker run creates a ...
Recently, the server has been frequently cracked ...
Here we only focus on the installation and use of...
1. Installation Environment Computer model: Lenov...
Uninstall MySQL 1. In the control panel, uninstal...
This article mainly summarizes some commonly used...
Table of contents background analyze Data simulat...