Mysql delete duplicate data to keep the smallest id solution

Mysql delete duplicate data to keep the smallest id solution

Search online to delete duplicate data and keep the data with the smallest ID. The method is as follows:

DELETE
FROM
  people
WHERE
  peopleName IN (
    SELECT
      peopleName
    FROM
      people
    GROUP BY
      peopleName
    HAVING
      count(peopleName) > 1
  )
AND peopleId NOT IN (
  SELECT
    min(peopleId)
  FROM
    people
  GROUP BY
    peopleName
  HAVING
    count(peopleName) > 1
)

When I use it myself, an error message is displayed:

delete from tb where id in (SELECT max(id) from tb GROUP BY user HAVING count(user)>1)

[Err] 1093 - You can't specify target table 'XXX' for update in FROM clause

It is not known what caused it yet.

Then find a way to distribute the operation. First, filter out the data with duplicate users, and then use max() to select the larger row:

SELECT max(id) from tb GROUP BY user HAVING count(user)>1

Then delete the redundant data one by one according to the obtained max(id)

delete from tb where id=xx

This is a stupid method, let’s solve the problem temporarily.

Summarize

The above is the solution for Mysql to delete duplicate data and retain the smallest ID introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time!

You may also be interested in:
  • Example and analysis of MySQL clearing data tables
  • MySQL query duplicate data (delete duplicate data and keep the one with the smallest id as the only data)
  • Detailed explanation of how to find and delete duplicate data in MySQL and keep only one example
  • Mysql implements regular clearing of old data in a table and retaining several pieces of data (recommended)

<<:  How to get the real path of the current script in Linux

>>:  Analysis of CocosCreator's new resource management system

Recommend

The principles and defects of MySQL full-text indexing

MySQL full-text index is a special index that gen...

Solution to the MySQL error "Every derived table must have its own alias"

MySQL reports an error when executing multi-table...

Detailed explanation of the watch listener example in vue3.0

Table of contents Preface The difference between ...

Difference and principle analysis of Nginx forward and reverse proxy

1. The difference between forward proxy and rever...

What to do if you forget the initial password of MySQL on MAC

The solution to forgetting the initial password o...

JavaScript to achieve progress bar effect

This article example shares the specific code of ...

Detailed tutorial on installing CentOS, JDK and Hadoop on VirtualBox

Table of contents 1. Prerequisites 1.1 Supported ...

Simple steps to create a MySQL container with Docker

Preface We have already installed Docker and have...

Use of Linux passwd command

1. Command Introduction The passwd command is use...

MySQL Innodb key features insert buffer

Table of contents What is insert buffer? What are...

A brief discussion on MySQL large table optimization solution

background The amount of new data in the business...

The iframe refresh method is more convenient

How to refresh iframe 1. To refresh, you can use j...