Deleting duplicate rows with sql

后端 未结 5 864
滥情空心
滥情空心 2021-01-17 03:59

I am trying to delete duplicate rows from my mysql table. I\'ve tried multiple queries but I am keep on getting this error: #1093 - You can\'t specify target table \'usa_cit

5条回答
  •  情书的邮戳
    2021-01-17 04:42

    Judging from your examples, when you say "duplicate", you mean "having the same combination of id_state and city_name", correct? If so after you have done removing the duplictes, I strongly suggest creating a UNIQUE constraint on {id_state, city_name}.

    To actually remove the duplicates, it is not enough to just identify the set of duplicates, you must also decide which of the identified duplicates to keep. Assuming you want to keep the ones with the smallest id, the following piece of SQL will do the job:

    CREATE TEMPORARY TABLE usa_city_to_delete AS
        SELECT id FROM usa_city T1
        WHERE EXISTS (
            SELECT * FROM usa_city T2
            WHERE
                T1.id_state = T2.id_state 
                AND T1.city_name = T2.city_name
                AND T1.id > T2.id
        );
    
    DELETE FROM usa_city
    WHERE id IN (SELECT id FROM usa_city_to_delete);
    
    DROP TEMPORARY TABLE usa_city_to_delete;
    

    Unfortunately, MySQL does not allow the correlated subqueries in DELETE, otherwise we could have done that in a single statement, without the temporary table.

    --- EDIT ---

    You can't have a correlated subquery but you can have JOIN, as illustrated by Carlos Quijano answer. Also, the temporary table can be created implicitly, as suggested by Kokers.

    So it is possible to do it in a single statement, contrary to what I wrote above...

提交回复
热议问题