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

WeChat applet implements a simple dice game

This article shares the specific code of the WeCh...

jQuery clicks on the love effect

This article shares the specific code of jQuery&#...

4 solutions to mysql import csv errors

This is to commemorate the 4 pitfalls I stepped o...

Detailed explanation of the usage of two types of temporary tables in MySQL

External temporary tables A temporary table creat...

How to view Docker container application logs

docker attach command docker attach [options] 容器w...

Solution to MySQL IFNULL judgment problem

Problem: The null type data returned by mybatis d...

How to configure jdk environment under Linux

1. Go to the official website to download the jdk...

JS+Canvas realizes dynamic clock effect

A dynamic clock demo based on Canvas is provided ...

Vue integrates Tencent Map to implement API (with DEMO)

Table of contents Writing Background Project Desc...

Docker data volume common operation code examples

If the developer uses Dockerfile to build the ima...

Deploy grafana+prometheus configuration using docker

docker-compose-monitor.yml version: '2' n...

CentOS 7 set grub password and single user login example code

There are significant differences between centos7...

Docker setting windows storage path operation

When installing Docker on Windows 10, after selec...

Nodejs module system source code analysis

Table of contents Overview CommonJS Specification...