mysql removes duplicates of a column in a specified table

If the table name is "aaaaa" and the column name is "name", you can use the following SQL statement to remove duplicates in the "name" column in the "aaaaa" table:

DELETE FROM aaaaa 
WHERE id NOT IN (
    SELECT id 
    FROM (
        SELECT MIN(id) as id 
        FROM aaaaa 
        GROUP BY name
    ) AS temp_table
);

In the above SQL statement, a temporary table named temp_table is first created, which only contains the smallest ID among the different values ​​of the "name" column in the "aaaaa" table, and then queries "aaaaa" in the external SELECT statement "For all the records in the table, only the records whose ID exists in temp_table are retained, that is, the record with the smallest ID among the duplicates in the "name" column is retained, and other records are deleted to achieve the purpose of deduplication.

Guess you like

Origin blog.csdn.net/u010522644/article/details/130103105