MySQL data duplicate checking and deduplication implementation statements

MySQL data duplicate checking and deduplication implementation statements

There is a table user, and the fields are id, nick_name, password, email, and phone.

1. Single field (nick_name)

Find all records with duplicate records

select * from user where nick_name in (select nick_name from user group by nick_name having count(nick_name)>1);

Find the record with the largest ID in each record group with duplicate records

select * from user where id in (select max(id) from user group by nick_name having count(nick_name)>1);

Find out the redundant records, but not the record with the smallest id

select * from user where nick_name in (select nick_name from user group by nick_name having count(nick_name)>1) and id not in (select min(id) from user group by nick_name having count(nick_name)>1);

Delete redundant duplicate records and keep only the record with the smallest id

delete from user where nick_name in (select nick_name from (select nick_name from user group by nick_name having count(nick_name)>1) as tmp1) and id not in (select id from (select min(id) from user group by nick_name having count(nick_name)>1) as tmp2);

2. Multiple fields (nick_name, password)

Find all the records with duplicate records

select * from user where (nick_name,password) in (select nick_name,password from user group by nick_name,password where having count(nick_name)>1);
 

Find the record with the largest ID in each record group with duplicate records

select * from user where id in (select max(id) from user group by nick_name,password where having count(nick_name)>1);

Find the redundant records in each duplicate record group, but do not find the one with the smallest ID.

select * from user where (nick_name,password) in (select nick_name,password from user group by nick_name,password having count(nick_name)>1) and id not in (select min(id) from user group by nick_name,password having count(nick_name)>1);

Delete redundant duplicate records and keep only the record with the smallest id

delete from user where (nick_name,password) in (select nick_name,password from (select nick_name,password from user group by nick_name,password having count(nick_name)>1) as tmp1) and id not in (select id from (select min(id) id from user group by nick_name,password having count(nick_name)>1) as tmp2);

The above is the detailed content of the implementation statements for MySQL data duplication checking and deduplication. For more information about MySQL data duplication checking and deduplication, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • MySQL development skills: JOIN update and data duplication check/deduplication
  • One sql statement completes MySQL deduplication and keeps one
  • MySQL deduplication methods
  • A brief discussion on deduplication in SQL database
  • A small example of SQL grouping and sorting to remove duplicates
  • Detailed explanation of two methods of deduplication in MySQL and example code
  • A practical record of how to check and remove duplicate SQL

<<:  A brief analysis of Linux resolv.conf

>>:  Various correct postures for using environment variables in Webpack

Recommend

js object-oriented method to achieve drag effect

This article shares the specific code for impleme...

The linkage method between menu and tab of vue+iview

Vue+iview menu and tab linkage I am currently dev...

Solution to transparent font problem after turning on ClearType in IE

The solution to the transparent font problem after...

HTML input box optimization to improve user experience and ease of use

In order to improve user experience and ease of us...

Vue opens a new window and implements a graphic example of parameter transfer

The function I want to achieve is to open a new w...

VMware Workstation download and installation detailed tutorial

Virtual machines are very convenient testing soft...

canvas.toDataURL image/png error handling method recommendation

Problem background: There is a requirement to tak...

A brief understanding of MySQL storage field type query efficiency

The search performance from fastest to slowest is...

Docker configures the storage location of local images and containers

Use the find command to find files larger than a ...

Handwriting implementation of new in JS

Table of contents 1 Introduction to the new opera...

Web Design Tutorial (6): Keep your passion for design

<br />Previous article: Web Design Tutorial ...

Details about the like operator in MySQL

1. Introduction When filtering unknown or partial...

Using JS to implement binary tree traversal algorithm example code

Table of contents Preface 1. Binary Tree 1.1. Tra...